为什么我的输出与另一个输出分开?

时间:2013-11-21 22:50:14

标签: java

我的输出正在与另一对输出分组,并且该输出根本不会打印。 根本不起作用的输入是(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 个答案:

答案 0 :(得分: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,添加一些断点并开始调试代码......这是非常宝贵的技能,你应该尽快开始学习;)