이 클래스에는 사각형 목록이 포함되어 있으며 가장 작은 영역으로 사각형을 찾아야합니다.두 개의 두 숫자를 비교하고 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;
}
어떻게이 문제를 해결하기 위해?
간단한 비교 연산자를 사용해보십시오. – rptwsthi
@rptwsthi 올바르게 작동하지 않았다 –
사각형 클래스에서 comparable을 구현하는 것은 어떻습니까? – misserandety