我正在设计一个项目,将用户结果与标准结果进行比较。我的算法无法完全按照我希望的方式运行(评分系统)。我的代码的问题在于,它根本不会以我希望的方式运行。它只会从第一个“ while”和“ if”条件中读取。它根本无法识别其他条件。年龄和分数将通过外部来源提供,因此这只是该工具的算法/评分系统。
import java.util.Scanner;
public class algorithm_tester {
int score;
int age;
public static void main(String[] args) {
int score = 0;
int age = 0;
Scanner input = new Scanner(System.in);
//User Inputs Score
System.out.print("Enter Your Score: ");
int myscore = input.nextInt();
System.out.println("Score entered = " + myscore);
//User Inputs Age
System.out.print("Enter Your Age: ");
int myAge = input.nextInt();
System.out.println("Age entered = " + myAge);
while (age < 30) {
if (score < 15) {
System.out.println("Your Score Is Slightly Abnormal For Your Age.");
} else if (score < 10) {
System.out.println("Your Results Are Rather Low. Therefore, We Recommend You Seek Medical Advice.");
} else if (score < 8) {
System.out.println("Your Results Suggest Severe Cognitive Impairment. You Must Seek Medical Attention Immediately.");
} else {
System.out.println("Well Done! You Scored Very Well.");
}
while (age < 40) {
if (score < 14) {
System.out.println("Your Results Are Slightly Abnormal For Your Age.");
} else if (score < 10) {
System.out.println("We Recommend You Seek Medical Advice As Your Results Are Quite Low.");
} else if (score < 7) {
System.out.println("Your Score Is Very Low. We Believe You May Have Experienced Majoe Cognitive Impairment. We Recommend You Seek Immediate Medical Attention.");
} else {
System.out.println("Well Done! You Have Shown No Evidence Cognitive Impairment");
}
while (age < 60) {
if (score < 12) {
System.out.println("Your Results Are Slightly Abnormal For Your Age.");
} else if (score < 9) {
System.out.println("Your Score is Very Low. We Believe This Is Due To Minor Cognitive impairment. We Recommend You Seek Medical Advice.");
} else if (score < 6) {
System.out.println("Your Results Have Led Us to Believe You Have Suffered Severe Cognitive Impairment. You Must Seek Medical Attention Immediately.");
} else {
System.out.println("Congratulations! Your Results Were Excellent. You Have Shown No Evidence Of Cognitive Impairment");
}
while (age < 80) {
if (score < 10) {
System.out.println("Your Results Are Slightly Abnormal For Your Age.");
} else if (score < 7) {
System.out.println("Your Score is Very Low. We Believe This Is Due To Minor Cognitive impairment. We Recommend You Seek Medical Advice.");
} else if (score < 5) {
System.out.println("Your Results Have Led Us to Believe You Have Suffered Severe Cognitive Impairment. You Must Seek Medical Attention Immediately.");
} else {
System.out.println("Congratulations! Your Results Were Excellent. You Have Shown No Evidence Of Cognitive Impairment");
}
while (age > 80) {
if (score < 10) {
System.out.println("Your Results Are Slightly Abnormal For Your Age.");
} else if (score < 6) {
System.out.println("Your Score is Very Low. We Believe This Is Due To Minor Cognitive impairment. We Recommend You Seek Medical Advice.");
} else if (score < 4) {
System.out.println("Your Results Have Led Us to Believe You Have Suffered Severe Cognitive Impairment. You Must Seek Medical Attention Immediately.");
} else {
System.out.println("Congratulations! Your Results Were Excellent. You Have Shown No Evidence Of Cognitive Impairment");
}
}
}
}
}
}
}
}
答案 0 :(得分:5)
那是因为您的while
循环是无限循环,并且因为您应该颠倒if
语句的顺序。您永远不会在任何循环内获得用户输入,因此无法更改值。例如,age < 30
一旦age
总是小于30,则此循环将永远不会结束,它将永远不会运行程序的任何其他部分在特定的while
循环中。
为此,您目前正在将age
和score
硬编码为0,并且从不更改它们-我假设您想在这里获得用户输入或其他信息。
不过,while
循环对于您要实现的目标根本没有意义。您实际上应该使用嵌套的if
语句,如下所示:
if (age < 30) {
if (score < 8) {
// ...
}
else if (score < 10) {
// ...
}
// Etc.
}
else if (age < 40) {
// ...
}
else if (age < 60) {
// ...
}
// Etc.
如果您要重复整个程序直到用户输入特定值(例如 then ),则将整个程序放入while
循环中进行检查是很有意义的。用户输入的值。
还要注意,我颠倒了嵌套if
语句的方向。如果分数小于8,则分数也将小于10且小于15,因此您要先检查分数是否小于8。
答案 1 :(得分:0)
您正在输入“ myscore”和“ myage”变量,而if和while条件完全基于不同的变量“ age”和“ score”,因为您为其分配了零,所以它总是到第一个if和while并且您的算法不起作用 把age = myage //输入后 和 分数= myscore //输入分数后