执行程序时,额外的输入命令会导致逻辑错误java

时间:2015-03-31 04:58:27

标签: java while-loop next

我正在尝试执行一个名为AverageRainfall的程序。大多数输入工作正常(我在开头的语句很好),但是变量monthRain下有多个月,而monthRain的while语句不能用于各个月,只有初始输入命令,即服务没有目的。

ETA:发布整个代码进行测试

  import java.util.Scanner; //for Scanner class

  public class AverageRainfall
  {
  public static void main(String[] args)
  {
     final int NUM_MONTHS = 12;     //Months per year
     int years;                     //Number of years
     double monthRain;                  //Rain for a month
     double totalRain = 0;              //Rainfall accumulator
     double average;                    //Average rainfall

     Scanner keyboard = new Scanner(System.in);

     {
     System.out.print("Enter the number of years: ");
     years = keyboard.nextInt();

     while (years < 1)
        {
        System.out.print("Invalid. Enter 1 or greater: ");
        years = keyboard.nextInt();
        }
     }

      { 
         System.out.println("Enter the rainfall, in inches, for each month. ");
         monthRain = keyboard.nextDouble();

         for(int y = 1; y <= years; y++){

           for(int m = 1; m <= NUM_MONTHS; m++){

       System.out.print("Year" + y + "month" + m + ": ");
       monthRain = keyboard.nextDouble(); 
       }
       }
       while (monthRain < 0)
      {
         System.out.print("Invalid. Enter 0 or greater: ");
         monthRain = keyboard.nextDouble();
      }
      }  

       {
        totalRain += monthRain;

        average = totalRain / (years * NUM_MONTHS);

        System.out.println("\nNumber of months: " + (years * NUM_MONTHS) );
        System.out.println("Total rainfall: " + totalRain + " inches");
        System.out.println("Average monthly rainfall: " + average + " inches");
       }
    }
}

这是整个代码。

2 个答案:

答案 0 :(得分:0)

每当用户输入一个月的降雨量时,你可以做的就是增加Rain的总数。然后,一旦他完成输入数据,你就可以做到平均值。

`import java.util.Scanner; 公共课堂考试{

public static void main(String[]args){
    double monthRain=0;
    double totalRain=0;
    Scanner keyboard = new Scanner(System.in);
    int years = 1;
    int NUM_MONTHS = 12;
    System.out.println("Enter the rainfall, in inches, for each month. ");
    for(int y = 1; y <= years; y++){
        for(int m = 1; m <= NUM_MONTHS; m++){

            System.out.print("Year" + y + "month" + m + ": ");
            monthRain = keyboard.nextDouble(); 
            totalRain+=monthRain;
        }
    }
    int totalMonth = years*NUM_MONTHS;
    System.out.println("\nNumber of months: " + totalMonth );
    System.out.println("Total Rain: "+totalRain+" inches");
    double average = totalRain / totalMonth;
    System.out.println("Average monthly rainfall: " + average + " inches");

}

} `

答案 1 :(得分:0)

你正在使用不必要的大括号。此外,您的代码中也存在一些逻辑缺陷。我修复了你的代码。请参考以下代码:

import java.util.Scanner; //for Scanner class

public class AverageRainfall {
 public static void main(String[] args) {
    final int NUM_MONTHS = 12; // Months per year
    int years; // Number of years
    double monthRain=0; // Rain for a month
    double totalRain = 0; // Rainfall accumulator
    double average; // Average rainfall

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the number of years: ");
    years = keyboard.nextInt();

    while (years < 1) {
        System.out.print("Invalid. Enter 1 or greater: ");
        years = keyboard.nextInt();

    }


    System.out.println("Enter the rainfall, in inches, for each month. ");
    for (int y = 1; y <= years; y++) {

        for (int m = 1; m <= NUM_MONTHS; m++) {

            System.out.print("Year" + y + "month" + m + ": ");
            monthRain = keyboard.nextDouble();

            while (monthRain < 0) {
                System.out.print("Invalid. Enter 0 or greater: ");
                monthRain = keyboard.nextDouble();
            }
            totalRain += monthRain;
        }   
    }   





    average = totalRain / (years * NUM_MONTHS);

    System.out.println("\nNumber of months: " + (years * NUM_MONTHS));
    System.out.println("Total rainfall: " + totalRain + " inches");
    System.out.println("Average monthly rainfall: " + average
                + " inches");

 }
}