2013-11-21 4 views
-3

내 출력이 다른 출력 쌍과 그룹화되어 출력이 전혀 인쇄되지 않습니다. 전혀 작동하지 않는 입력은 (f, i)이고 그룹화 된 쌍 입력은 (c, c)입니다.출력이 다른 출력과 다른 이유는 무엇입니까?

* 나는 이것이 if() 진술과 관련이 있다고 생각합니다.

import java.util.Scanner; 
class AnnualClimate{ 

public static void main (String [] args) 
{ //Declare and intialize variables - programmer to provide initial values 
    Scanner in = new Scanner(System.in); 
    String city = "Daytona Beach"; 
    String state = "Florida"; 
    int o=0; 
    int u=0; 
    int y=0; 
    int t=0; 
    int z=0; 
    int m=0; 
    double [] celsius;  
    int i = 0; 
    int index=0; 
    double [] average1c; 
    double [] average2c; 
    double [] average1; 
    double [] average2; 
    String month [] ={"Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; 
    double temperature [] = {58.4,60.0,64.7,68.9,74.8,79.7,81.7,81.5,79.9,74.0,67.0,60.8};   
    double precipitation [] ={3.1, 2.7,3.8,2.5,3.3,5.7,5.2,6.1,6.6,4.5,3.0,2.7};  
    String tempLabel = "F"; //initialize to F 
    String precipLabel = "inch"; //initialize to inch    

    //INPUT - ask user for temp and preciptation scale choice 
    System.out.print("Choose the temperature scale (F = Fahrenheit, C = Celsius): "); 
    String tempChoice = in.next(); 
    System.out.print("Choose the precipitation scale (i = inches, c = centimeteres): "); 
    String precipChoice = in.next(); 

    //PROCESSING - convert from F to C and in to cm based on user's choices 

    // remember 5/9 = 0, 5.0/9 = .5555 
    average1c = new double[temperature.length]; 
    celsius = new double[temperature.length]; 
    average2c = new double[precipitation.length]; 
    average1 = new double[temperature.length]; 
    average2 = new double[temperature.length]; 

    if(tempChoice.equalsIgnoreCase("C")){ 
     tempLabel="(C)"; 
     for(index = 0; index < temperature.length;) 
     { 
      celsius[index]= (temperature[index]-32) * 5/9; 
      average1[index]= celsius[index]/12; 
      index++;    
     } 
    } 

    //Convert in values to cm; replace the current values in precipitation 
    double[] centimeters = new double[ precipitation.length ]; 
    if(precipChoice.equalsIgnoreCase("c")) 
    { 
     precipLabel="(cm)"; 
     for (i= 0; i<precipitation.length; i++) 
     { 
      centimeters[i]= precipitation[i]* 2.54; 
      average2[i]=centimeters[i]/12; 
     } 
    } 

     for(z=0; z < temperature.length; z++) 
     { 
      average1[z]= temperature[z]/12; 
     } 
     for(m=0; m < temperature.length; m++) 
     { 
      average1[m]= temperature[m]/12; 
     } 
    //OUTPUT - print table using printf to format and align data 

    System.out.println(); 
    System.out.println("Climate Data"); 
    System.out.println("Location: " + city +", " + state); 
    System.out.printf("%5s %18s %s %18s %s","Month","Temperature",tempLabel,"Precipitation",precipLabel); 
    System.out.printf("%n"); 
    System.out.printf("***************************************************"); 

    while (o< month.length){    
    if(tempChoice.equalsIgnoreCase("C")){ 
     System.out.printf("%n"); 
     System.out.printf(month[o]); 
     System.out.printf("%20.2f", celsius[o]); 
     System.out.printf("%25.2f", precipitation[o]); 
     o++; 
    }  
} 

while (u< month.length){    
    if(tempChoice.equalsIgnoreCase("c") & (precipChoice.equalsIgnoreCase("c"))){ 
     System.out.printf("%n"); 
     System.out.printf(month[u]); 
     System.out.printf("%20.2f", celsius[u]); 
     System.out.printf("%25.2f", centimeters[u]); 
     u++; 
    }  
}  
while (y< month.length){ 

    if(tempChoice.equalsIgnoreCase("f") & (precipChoice.equalsIgnoreCase("c"))) 
    { 
     System.out.printf("%n"); 
     System.out.printf(month[y]); 
     System.out.printf("%20.2f",temperature [y]); 
     System.out.printf("%25.2f", centimeters[y]); 
     y++; 
    }  
} 

    while (t< month.length){ 
    if(tempChoice.equalsIgnoreCase("f") & (precipChoice.equalsIgnoreCase("i"))){ 
     System.out.printf("%n"); 
     System.out.printf(month[t]); 
     System.out.printf("%20.2f",temperature [t]); 
     System.out.printf("%25.2f", precipitation[t]); 
     t++; 
    }  
} 
    System.out.println(); 
    System.out.printf("***************************************************"); 
    System.out.println(); 

    while (y< month.length) 
    {    
    if(tempChoice.equalsIgnoreCase("f") & (precipChoice.equalsIgnoreCase("c"))) 
    { 
     System.out.printf("%n");    
     System.out.printf("%20.2f", average1); 
     System.out.printf("%25.2f", average2c); 
     y++; 
    } 
} 
+1

아주 작은 방향으로 진행하는 코드입니다. 시의 적절하고 철저한 답을 제공하는 데 도움이되도록 가능한 한 적은 코드로 오류를 복제하고 응용 프로그램이 얻으려는 내용 (단순한 결과가 아님)에 대한 설명을 보내 주시면 감사하겠습니다. '(f, i)'와'(c, c)'는 아마 당신에게 의미가 있지만, 우리에게는 아무것도 아닙니다. –

+0

@user 다음 번에 원하지 않는 공백을 모두 제거하고 올바른 형식으로 코드를 작성하십시오. 그렇게 쉽게 읽을 수 있습니다. – Adarsh

답변

0

이것은 디버거를위한 작업입니다.

첫 번째 문제는 tempChoice 당신이 끝없는 루프에 자신을 얻을 의미 F 다음 o가 업데이트되지 않습니다이다

while (o < month.length) { 

    if (tempChoice.equalsIgnoreCase("C")) { 
     System.out.printf("%n"); 
     System.out.printf(month[o]); 
     System.out.printf("%20.2f", celsius[o]); 
     System.out.printf("%25.2f", precipitation[o]); 
     o++; 
    } 
} 

경우 "달"루프입니다.

난 당신이 예를 들어, 대신 for-loop를 사용하는 것이 좋습니다 것입니다 ...

for (o = 0; o < month.length; o++) { 

    if (tempChoice.equalsIgnoreCase("C")) { 
     System.out.printf("%n"); 
     System.out.printf(month[o]); 
     System.out.printf("%20.2f", celsius[o]); 
     System.out.printf("%25.2f", precipitation[o]); 
    } 

} 

그런 다음 시작해야 이것은 매우 중요한 기술입니다 ... 일부 중단 점을 추가하고 당신에게 코드를 디버깅을 시작 좋은 IDE를 사용하여 최대한 빨리 배우기;)

관련 문제