2013-11-21 65 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

這是一個調試器的工作。

你的第一個問題是你的「月」循環

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++; 
    } 
} 

如果tempChoiceF然後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,添加一些斷點並開始調試你的代碼...這是非常寶貴的技能,你應該開始儘快學習;)