gpt4 book ai didi

java - 无法找出逻辑错误

转载 作者:行者123 更新时间:2023-12-02 05:55:35 24 4
gpt4 key购买 nike

import java.util.Scanner;

public class TestScoreApp { public static void main(String[] args) {
// initialize variables and create a Scanner object
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
String choice = "y";
Scanner sc = new Scanner(System.in);

// get a series of test scores from the user
while(choice.equalsIgnoreCase("y"))
{
System.out.println("How many testscores would you like to enter?");
int numberOfTestScores = sc.nextInt();

// display operational messages
System.out.println("Please enter test scores that range from 0 to 100.");
System.out.println(); // print a blank line

for(int i = 0; i == numberOfTestScores; i++)
{
// get the input from the user
System.out.print("Enter score " + numberOfTestScores + ": ");
testScore = sc.nextInt();

// accumulate score count and score total
if (testScore <= 100)
{
scoreCount = scoreCount + 1;
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.println("Would you like to enter another set of scores? y/n");
sc.next(choice);
System.out.println();
}
}
}


}

上面的代码是让用户输入测试成绩。他们首先选择要参加的人数。使用 for 循环来确保它们输入正确的数量。之后输出分数、总分数和平均分数。我当前正在获取输出:

    How many testscores would you like to enter?
34
Please enter test scores that range from 0 to 100.

How many testscores would you like to enter?
43
Please enter test scores that range from 0 to 100.

How many testscores would you like to enter?
3
Please enter test scores that range from 0 to 100.

How many testscores would you like to enter?

如果有人解释不让 while 循环的控制过渡到 for 循环背后的逻辑错误,我将不胜感激。

最佳答案

您没有获得任何测试分数,因为 for 循环的条件错误。从一开始就是false,只有当条件为true时循环才会循环。改变

for(int i = 0; i == numberOfTestScores; i++)

for(int i = 0; i < numberOfTestScores; i++)

关于java - 无法找出逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23121381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com