2016-11-04 1 views
0

펜싱을 100 야드 만 사용하는 경우 울타리가있는 영역에서 얻을 수있는 최대 영역을 계산하는 프로그램을 만들고 있습니다. 네면 중 하나는 이미 존재하는 임의의 긴 울타리로 정의됩니다. 정답 (최대 면적)은 1250 야드입니다. 프로그램을 럼주 때 얻고 있습니다. 그러나, 나는 또한 그 값과 함께 가기 위해 너비와 길이 치수를 출력해야하고, 나는 을 매우 (이상은 25와 50)으로하는 이상한 숫자로 만들고있다. 아무도 이것이 왜 일어나는지 설명 할 수 있습니까? 어떻게 해결할 수 있습니까? 정확한 최종 값을 얻고 있지만 잘못된 숫자가 있습니다.

public class Prog215c{ 
    public static void main (String[] args){ 
    //Initalizing the final dimension variables 
    double fin_l = 0; 
    double fin_w = 0; 

    //Initalizing the area variables 
    double area = 0; 
    double prev_area = 0; 
    double max_area = 0; 

    //Trying all l values 
    for (double l = 100; l > 0; l--){ 

     //Calculating the w value from the l value 
     double w = (100-l)/2; 

     //Calculating the area 
     area = l * w; 

     if (area >= prev_area){ 
       prev_area = area; 

       } 

       else{ 
        max_area = prev_area; 
        fin_l = l; 
        fin_w = w; 

       } 
    } 
    //Outprinting the results 
    System.out.println("With 100 yards of fencing material:"); 
    System.out.println("A rectangle " + fin_w + " X " + fin_l + " yards produces the maximum area of " + 
    max_area + " square yards."); 


} 
    } 


    /*Sample Output 
    With 100 yards of fencing material: 
    A rectangle 49.5 X 1.0 yards produces the maximum area of 1250.0   square yards. 


    */ 

이 1250.0가

+0

무엇이'그리고 나는 그 값들에 대해 매우 이상한 숫자를 얻고있다 .' –

+0

@ 엘리 사도 프 : 문제를 해결하는 방법에 대한 아이디어가 있습니까? –

+0

@ Skary Wombat : 출력을 하단에 포함 시켰습니다 (약간 스크롤해야 할 수도 있습니다). –

답변

0

그것은

if (area >= prev_area){ 
    prev_area = area; 
    fin_l = l; 
    fin_w = w; 
} 
+0

매력처럼 작동했습니다! 고마워, 무서운 Wombat! –

0

변경으로 변수를 설정하는 더 의미 (스택 오버플 포맷) 후에 공간 무시이 :

if (area >= prev_area){ 
      prev_area = area; 
} 

else{ 
     max_area = prev_area; 
     fin_l = l; 
     fin_w = w; 

      } 

하여 이 :

if (area >= prev_area){ 
      prev_area = area; 
      fin_l = l; 
      fin_w = w; 
      } 

      else{ 
       max_area = prev_area; 


      } 
+0

oups가 너무 늦은 것 같습니다. – NewBie1234

관련 문제