简体   繁体   中英

when executing program, extra input command causes logical errors java

I am trying to execute a program called 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.

ETA: Posting entire code for testing

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

 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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