简体   繁体   English

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

[英]when executing program, extra input command causes logical errors java

I am trying to execute a program called AverageRainfall. 我正在尝试执行一个称为AverageRainfall的程序。 Most of the input works well (my while statement at the beginning is fine), but there are multiple months under the variable monthRain, and the while statement for monthRain is not functioning with the various months, only the initial input command, which is serving no purpose. 大部分输入都能很好地工作(开头的my while语句很好),但是变量monthRain下有多个月份,monthRain的while语句在各个月份中均不起作用,只有初始输入命令在起作用没有目的

ETA: Posting entire code for testing 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");
       }
    }
}

This is the entire code. 这是完整的代码。

What you can do is add to the total of Rain each time the user inputs a month rain. 您可以做的是在用户每次输入一个月的降雨时将其添加到降雨总数中。 Then you can do the average once he finishes inputting data. 然后,当他完成数据输入后,便可以进行平均。

`import java.util.Scanner; `import java.util.Scanner; public class test { 公开课测试{

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");

}

} ` }`

You were using unnecessary braces. 您正在使用不必要的括号。 Moreover, some logical flaws were also present in your code. 此外,您的代码中还存在一些逻辑缺陷。 I have fixed your code. 我已经修复了您的代码。 Please refer the following code: 请参考以下代码:

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");

 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM