한 가지 방법은 아래처럼 명명된 변수를 생성하는 것입니다.

    int roundedToPercent = Math.toIntExact(Math.round(averageFuel * 100));
    return roundedToPercent;

    코드는 명확해졌고 주석은 제거되었습니다. 하지만 메서드에 추가한 변수가 꽤 불필요합니다. 바로 변환되니까요.

    더 나아질 수 있는지 봅시다!

    class FuelSystem {
    
        List<Double> tanks = new ArrayList<>();
    
        int getAverageTankFillingPercent() {
            double sum = 0;
            for (double tankFilling : tanks) { 
                sum += tankFilling;
            }
            double averageFuel = sum / tanks.size();
            return roundToIntegerPercent(averageFuel);
        }
    
        static int roundToIntegerPercent(double value) {
            return Math.toIntExact(Math.round(value * 100));
        }
    }
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.