我已經看過幾個平均計算器,但沒有一個具有這個特定功能。 基本上,我想問它「你想要平均多少個數字?」然後「輸入您的電話號碼」,並在每次輸入後繼續提示「輸入您的電話號碼」,直到「多少個號碼......」數量滿足爲止。我知道這是一個循環計數(對不起,如果我的行話是關閉的......我只在我的第二學期的計算機編程),但我不知道如何設置它。預先感謝您的答案。這是我到目前爲止有:具有定義條目數量的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();
}
}
}
好吧,看起來你已經有了一個循環。爲什麼不給它添加適當的條件,以便在閱讀所需數量的分數後停止? – RealSkeptic