2016-09-12 26 views
0

我已經看過幾個平均計算器,但沒有一個具有這個特定功能。 基本上,我想問它「你想要平均多少個數字?」然後「輸入您的電話號碼」,並在每次輸入後繼續提示「輸入您的電話號碼」,直到「多少個號碼......」數量滿足爲止。我知道這是一個循環計數(對不起,如果我的行話是關閉的......我只在我的第二學期的計算機編程),但我不知道如何設置它。預先感謝您的答案。這是我到目前爲止有:具有定義條目數量的Java平均計算器

import java.util.Scanner; 

public class TestScoreApp 
{ 
    public static void main(String[] args) 
    { 
     // welcome the user to the program 
     System.out.println("Welcome to the Test Average Calculator!"); 
     System.out.println(); // print a blank line 
     // display operational messages 
     System.out.println("Please enter test scores that range from 0 to 100."); 
     System.out.println(); // print a blank line 

     // initialize variables and create a Scanner object 
     int scoreTotal; 
     int scoreCount; 
     int testScore; 
     Scanner sc = new Scanner(System.in); 

     // perform calculations until choice isn't equal to "y" or "Y" 
     String choice = "y"; 
     while (!choice.equalsIgnoreCase("n")) 
     { 
      // get the number of grades to be averaged from the user 
      System.out.print("How many scores would you like to average? "); 
      scoreCount = sc.nextInt(); 

      // get the input from the user 
      System.out.print("Enter score: "); 
      testScore = sc.nextInt(); 

      // accumulate score count and score total 
      if (testScore <= 100) 
      { 
       scoreTotal = scoreTotal + testScore; 
      } 
      else if (testScore >= 100) 
       System.out.println("Invalid entry, not counted"); 


     // display the score count, score total, and average score 
     double averageScore = scoreTotal/scoreCount; 
     String message = "\n" + 
         "Score count: " + scoreCount + "\n" 
         + "Score total: " + scoreTotal + "\n" 
         + "Average score: " + averageScore + "\n"; 
     System.out.println(message); 

     System.out.print("Would you like to average more grades? (y/n): "); 
      choice = sc.next(); 
      System.out.println(); 
     } 
    } 
} 
+1

好吧,看起來你已經有了一個循環。爲什麼不給它添加適當的條件,以便在閱讀所需數量的分數後停止? – RealSkeptic

回答

0

你的方法幾乎是正確的,除了一些錯誤。您想要輸入,直到按下'n',然後顯示平均值。這意味着平均值計算必須在循環之外完成,當輸入結束時。

如果你想利用輸入與輸入而不是「Y」的預定數量/「N」的辦法,你可以重用while循環:

int numOfInput = sc.nextInt(); // how many number will be entered 
while(numOfInput > 0) { 
    // take every input and add to total 
    --numOfInput; 
} 
// average calculation 

而且,一點點在輸入驗證邏輯錯誤檢查。

if (testScore <= 100) // for less or equal 100 
{ 
    scoreTotal = scoreTotal + testScore; 
} 
else if (testScore >= 100) // for greater or equal 100 
    System.out.println("Invalid entry, not counted"); 

兩個條件檢查的數量是否等於100,這是不期望。如果允許小於100只數,那麼你可以寫:

if (testScore < 100) { 
    scoreTotal += testScore; 
} 
else { 
    System.out.println("Invalid entry, not counted"); 
} 
+0

非常感謝大家!這非常有幫助!肯定給了我一些選擇。我會嘗試一些更改,看看我能否讓他們工作。 – digiteeb

0

所以,你要平均scoreCount項目,或保持平均,直到用戶輸入「N」

如果這是第一種情況(正如你在你的問題中所描述的那樣)並且你想平均爲scoreCount次,那麼你需要改變while循環的條件。

System.out.print("How many scores would you like to average? "); 
scoreCount = sc.nextInt(); 
scoreTotal = 0; 
for(int i=0; i<scoreCount; i++){ 
    scoreTotal += sc.nextInt(); 
    System.out.print("Okay please enter next...");   
} 
System.out.print("Average is " + scoreTotal/scoreCount); 

如果你想用一個while做到這一點,只是不停的指數,int index=0;,增加每次迭代的索引和查詢,如果你已經超過了指數。

while (index < scoreCount) 
    scoreTotal += sc.nextInt(); 
    index++; 
System.out.print("Average is " + scoreTotal/scoreCount);  
0

這就是你需要:

for(int i = 0; i < scoreCount; i++){ 
    System.out.print("Enter score: ");  
    testScore = sc.nextInt(); 
} 

for循環創建整數保持其循環指數

int i; 

而且每個循環要求的是大於得分計數,如果不再循環。

i < scoreCount; 

而且每次循環後,它增加了一個

i++