2013-08-30 2 views
0

이 클래스에는 사각형 목록이 포함되어 있으며 가장 작은 영역으로 사각형을 찾아야합니다.두 개의 두 숫자를 비교하고 smaler를 찾는 방법

사각형을 영역별로 비교해야하지만 이중 정밀도가 있습니다. 제 비교는 마지막 것을 기억하고 있습니다 만, 여기서 어떻게 확인할 수 있습니까?

코드 :

/** 
    * Gets the Rectangle with the smallest area 
    * @return the rectangle with the smallest area or null if 
    * there are no rectangles 
    */ 
    public Rectangle smallestArea() 
    {   
     if (list.size() == 0) return null; 

     Rectangle smallest = list.get(0);   
     double smallestArea = smallest.getWidth() * smallest.getHeight(); 

     for (int i = 1; i < list.size(); i++) { 
      Rectangle next = list.get(i); 
      double nextArea = next.getWidth() * next.getHeight(); 

      if ((nextArea - smallestArea) < 0) smallest = next;    
     } 

     return smallest; 
    } 

어떻게이 문제를 해결하기 위해?

+1

간단한 비교 연산자를 사용해보십시오. – rptwsthi

+0

@rptwsthi 올바르게 작동하지 않았다 –

+0

사각형 클래스에서 comparable을 구현하는 것은 어떻습니까? – misserandety

답변

1

당신은 (코드에서 한 줄 이상)뿐만 아니라 smallestArea 지역 변수를 업데이트해야 할 것 :

public Rectangle smallestArea() 
    {   
     if (list.size() == 0) return null; 

     Rectangle smallest = list.get(0);   
     double smallestArea = smallest.getWidth() * smallest.getHeight(); 

     for (int i = 1; i < list.size(); i++) { 
      Rectangle next = list.get(i); 
      double nextArea = next.getWidth() * next.getHeight(); 

      if ((nextArea - smallestArea) < 0) { 
      smallest = next;   // <- Whenever you've updated smallest   
      smallestArea = nextArea; // <- Do not forget updating the smallestArea as well 
      } 
     } 

     return smallest; 
    } 
0

나는

double smallest = Double.POSITIVE_INFINITY; 

// in the loop. 
if (smallest > next) 
    smallest = next; 
1

당신이로 smallestArea를 업데이트해야하기 때문에 귀하의 알고리즘이 작동하지 않습니다 잘 :

if ((nextArea - smallestArea) < 0) { 
    smallest = next; 
    smallestArea = nextArea; // <<== Here 
} 

주을 그 (210)는 nextArea < smallestArea을 말하는 또 다른 방법입니다, 그래서 청소기이 보인다 :

if (nextArea < smallestArea) { 
    smallest = next; 
    smallestArea = nextArea; 
} 
0

smalles 사각형이 무엇인지 기억 외에, 당신은 또한 그것의 크기는 기억해야합니다. 루프 내부의 if 변경 :

if (nextArea < smallestArea) { 
    smallest = next; 
    smallestArea = nextArea 
} 
0
public Rectangle smallestArea() 
{   
    if (list.size() == 0) return null; 

    Rectangle smallest = list.get(0);   
    double smallestArea = smallest.getWidth() * smallest.getHeight(); 

    for (int i = 1; i < list.size(); i++) { 
     Rectangle next = list.get(i); 
     double nextArea = next.getWidth() * next.getHeight(); 

     if ((nextArea - smallestArea) < 0){ 
      smallest = next;    
      smallestArea = nextArea; 
     } 
    } 

    return smallest; 
} 
-1

보십시오, 이것에 대해

int retval = Double.compare(nextArea, smallestArea); 

if(retval < 0) 
{ 
    System.out.println("smallestArea is greater than nextArea"); 
    smallest = next; 
} 
+0

단순히 'Double.compare'사용하기를 원하기 때문에 두 개의 'double'값 중 작은 값이 불필요하게 복잡하지 않은지 확인하십시오. 그들을 비교하기 위해서'<'를 사용하십시오. 게다가, 이것은 nazar_art의 코드에서 문제가되지 않습니다. – Jesper

+0

@Jesper 귀하의 배려에 감사드립니다. – newuser

0

무엇을? 나는이 방법을 시도해 볼 수 있다고 생각합니다.

List<Rectangle> list=new ArrayList<>(); 
    List<Double> areaList=new ArrayList<>(); 
    for(Rectangle r:list){ 
     areaList.add(r.getHeight()*r.getHeight()); 
    } 
    Collections.sort(areaList); 
    System.out.println("Smallest "+areaList.get(0)); 
관련 문제