2013-11-21 3 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++; 
    } 
} 

Если tempChoice является F то 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, добавить некоторые контрольные точки и начать отладку коды вам ... Это в бесценном навыке, вы должны начать обучение ASAP;)

Смежные вопросы