2015-03-31 34 views
0

我想執行一個名爲AverageRainfall的程序。大部分的輸入效果很好(開始時我的while語句很好),但變量monthRain下有多個月,並且monthRain的while語句在各個月份不起作用,只有初始輸入命令正在服務沒有目的。執行程序時,額外的輸入命令會導致邏輯錯誤java

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

這是整個代碼。

+0

可以發佈整個代碼,以便我可以測試它嗎? – 2015-03-31 05:03:55

+0

是發佈完整的代碼。很難猜測。但我認爲最近發生的事情是totalRain + = monthRain;不在循環中,這就是所有月份都沒有考慮到的原因 – Yantraguru 2015-03-31 05:19:45

+0

@Kognizant我會如何將它放在循環中? – LizzySmit 2015-03-31 05:42:16

回答

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

} 
} 
+0

最終代碼中缺少一個大括號,但在集成它時已經考慮到了這一點。非常感謝,完美的工作 – LizzySmit 2015-03-31 06:09:48

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

} 

} `

+0

使用代碼時,我仍然遇到同樣的問題,它說它在兩年內只下了一英寸。 – LizzySmit 2015-03-31 05:27:27

+0

我在System.out.println中修正了一些東西,第一個應該是「Total Rain」。你使用什麼輸入? – 2015-03-31 05:29:36

+0

如果你現在執行代碼並輸入1,所有月份,你會得到總雨= 12,平均下雨= 1 – 2015-03-31 05:33:56