已计算Java无效项
我试图找出如何不包括被计数的无效条目。 我需要输入5个分数,并希望“分数计数”为5,但我所做的代码只输入4个“分数计数”,包括无效的输入。我不需要输入无效条目,也不知道如何将无效条目排除在分数之外 下面是代码已计算Java无效项,java,Java,我试图找出如何不包括被计数的无效条目。 我需要输入5个分数,并希望“分数计数”为5,但我所做的代码只输入4个“分数计数”,包括无效的输入。我不需要输入无效条目,也不知道如何将无效条目排除在分数之外 下面是代码 import java.util.Scanner; public class TestScoreApp { public static void main(String[] args) { // display operational messages
import java.util.Scanner;
public class TestScoreApp
{
public static void main(String[] args)
{
// display operational messages
System.out.println("Please enter test scores that range from 0 to 100.");
System.out.println("To end the program enter 999.");
System.out.println(); // print a blank line
Scanner sc = new Scanner(System.in);
String choice = "y";
// get a series of test scores from the user
while (!choice.equalsIgnoreCase("n"))
{
// initialize variables
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
System.out.println("Enter the number of test score to be entered: ");
int numberOfTestScores = sc.nextInt();
for (int i = 1; i <= numberOfTestScores; i++)
{
// get the input from the user
System.out.print("Enter score " + i + ": ");
testScore = sc.nextInt();
// accumulate score count and score total
if (testScore <= 100)
{
scoreCount = scoreCount + 1;
scoreTotal = scoreTotal + testScore;
}
else if (testScore != 999)
System.out.println("Invalid entry, not counted");
sc.nextLine();
// 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.println();
System.out.println("Enter more test scores? (y/n)");
choice= sc.next();
}
}
}
import java.util.Scanner;
公共类TestScoreApp
{
公共静态void main(字符串[]args)
{
//显示操作信息
System.out.println(“请输入从0到100的测试分数”);
System.out.println(“要结束程序,请输入999”);
System.out.println();//打印一个空行
扫描仪sc=新的扫描仪(System.in);
字符串选择=“y”;
//从用户处获取一系列测试分数
而(!choice.equalsIgnoreCase(“n”))
{
//初始化变量
int scoreTotal=0;
积分计数=0;
int testScore=0;
System.out.println(“输入要输入的测试分数:”);
int numberOfTestScores=sc.nextInt();
对于(int i=1;i而言,如果循环迭代变量i
是无效的分数,则只需将其减小一点,因此您需要该分数。因此,请更改此项:
else if (testScore != 999)
System.out.println("Invalid entry, not counted");
为此:
else if (testScore != 999) {
System.out.println("Invalid entry, not counted");
i--;
}
嘿,快6秒。+1。