2014-11-20 64 views
2

我想獲得用戶輸入數字的平均值。我每次輸入數字輸出是始終在線Java中用戶平均輸入的數字

總和爲6個 多少個數字:4 平均:2.0

A,而需要循環,不幸的是,循環都是不允許的。我對Java很新,所以我的格式很瑣碎。我的代碼在哪裏是我的問題?

import java.util.Scanner; 

public class LoopsEndingRemembering { 

    public static void main(String[] args) { 
     // program in this project exercises 36.1-36.5 
     // actually this is just one program that is split in many parts 

     Scanner reader = new Scanner(System.in); 
     int count = 0; 
     int sum = 0; 
     int endingVariable = -1; 
     int input = 0; 
     double average; 

     while (input >= endingVariable) { 
      System.out.println("Type numbers: "); 
      input = Integer.parseInt(reader.nextLine()); 
      sum += count; 
      average = (double)sum/count; 
      count++; 

      if (input == endingVariable) { 
       System.out.println("Thank you and see you later!"); 
       System.out.println("The sum is " + sum); 
       System.out.println("How many numbers: " + count); 
       System.out.println("Average: " + average); 
       break; 
      } 
     } 
    } 
} 
+4

你使用你的調試器,並通過該程序?你爲什麼要把'count'加到'sum'而不是'input'?你從來沒有對「輸入」做任何事情!另外,在計算平均值而不是計算平均值2時,計算平均值*之前,計算平均值時,計數仍爲「1」。 – tnw 2014-11-20 17:06:17

+0

[如何從給定值獲取平均值](http:// stackoverflow。com/questions/15516797/how-to-get-average-from-given-values) – jbutler483 2014-11-20 17:07:58

回答

0

你的代碼有兩個大問題。首先,這條線:

sum += count; 

這可能只是一個錯字,但它應該是明顯的:

sum += input; 

其次,一旦你解決這個問題,你會發現你是包括-1您的總和。你可能想要這樣的東西:

System.out.println("Type numbers: "); 
    input = Integer.parseInt(reader.nextLine()); 

    if (input == endingVariable) { 
     average = (double) sum/count; 

     System.out.println("Thank you and see you later!"); 
     System.out.println("The sum is " + sum); 
     System.out.println("How many numbers: " + count); 
     System.out.println("Average: " + average); 
     break; 
    } else { 
     sum += input; 
     count++; 
    } 
0

試試這個...

import java.util.Scanner; 

public class LoopsEndingRemembering { 

public static void main(String[] args) { 
    // program in this project exercises 36.1-36.5 
    // actually this is just one program that is split in many parts 

    Scanner reader = new Scanner(System.in); 

    int count = 0; 
    int sum = 0; 
    int endingVariable = -1; 
    int input = 0; 
    double average; 



    while (input >= endingVariable) { 


     System.out.println("Type numbers: "); 
     input = Integer.parseInt(reader.nextLine()); 
     sum += input; // you were doing mistake here. 
     average = (double)sum/count; 
     count++; 

     if (input == endingVariable) { 
      System.out.println("Thank you and see you later!"); 
      System.out.println("The sum is " + sum); 
      System.out.println("How many numbers: " + count); 
      System.out.println("Average: " + average); 
      break; 

     } 

    } 

} 
} 
3

您在這裏有幾個錯誤。

導致錯誤的主要原因是sum += count。不是添加用戶輸入的內容,而是添加顯示有多少個數字的數字。因此,它會隨時添加0 + 1 + 2 + 3等

的另一個問題是,一旦你改變上述情況,你必須檢查是否input == endingVariable,然後輸入要添加的總和。所以你必須先寫if,然後計算裏面的平均值。否則它會將您的-1視爲要計算的數字序列的一部分。

如果您發現輸入與endingVariable不相同,則只需將輸入添加到總和中並遞增計數。

所以:

  • 添加的輸入,而不是數量,以總和​​。
  • 只有當您達到最終項目時才計算平均值。
  • 只有當您輸入的不是endingVariable時,纔會將輸入添加到總和中並遞增計數器。
+0

也應該使用雙打。 – tnw 2014-11-20 17:14:34

+0

@tnw不一定。只要你剛剛添加,使用'int'或'long'就沒問題。當你來到平均部分時,你只需要'雙',這隻做一次。或者如果你的總和有這麼高的機會,它會溢出。 – RealSkeptic 2014-11-20 17:16:52

+0

謝謝你詳細解釋。它現在工作正常,我只需要做的就是改變輸入,然後移動sum + = input並在最後一項之後進行計數。我正在努力弄清楚爲什麼我的-1被包括在內。 – 2014-11-20 17:22:23

0

謝謝你詳細解釋。它現在工作正常,我只需要做的就是改變輸入,然後移動sum + = input並在最後一項之後進行計數。我正在最困難的時間試圖找出爲什麼我的-1被包括在內

相關問題