我见过几个平均计算器,但没有一个具有这个特定的功能。 基本上,我想问一下“你想平均多少个数字?”然后“输入您的号码”并在每次输入后继续提示“输入您的号码”,直到满足“多少号...”数量。我知道这是一个计数循环(对不起,如果我的行话是关闭的......我只是在我的第二学期计算机编程)但我不知道如何设置它。提前感谢您的回答。这是我到目前为止所做的:
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();
}
}
}
答案 0 :(得分: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);
答案 1 :(得分: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");
}
答案 2 :(得分:0)
这就是你所需要的:
for( int i = 0; i < scoreCount; i++){
System.out.print("Enter score: ");
testScore = sc.nextInt();
}
for循环创建整数 i 以保存其循环索引
int i;
每个循环要求 i 大于 scoreCount ,如果不再循环。
i < scoreCount;
在每个循环之后,它将一个添加到 i 。
i++