gpt4 book ai didi

java - 如果用户输入的整数不符合我的条件,如何结束控制台?

转载 作者:行者123 更新时间:2023-12-01 18:01:37 25 4
gpt4 key购买 nike

我目前正在创建一个简单的成绩计算器,但是当用户未输入 0 到 20 之间的数字时,我希望控制台不要继续下一行。即使用户在下一行输入了错误的数字被执行。也请随意提出使我的程序更简单的方法。

import java.util.Scanner;

public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter grade for Exam 1:>");
int exam1 = scanner.nextShort();
while (exam1 > 20 || exam1 < 0) {
System.out.println("Error! Not within mark range!");
break;
}

System.out.println("Enter grade for Exam 2:>");
int exam2 = scanner.nextShort();
while (exam2 > 20 || exam2 < 0) {
System.out.println("Error! Not within mark range!");
break;
}

System.out.println("Enter grade for Exam 3:>");
int exam3 = scanner.nextShort();
while (exam3 > 20 || exam3 < 0) {
System.out.println("Error! Not within mark range!");
break;
}

System.out.println("Enter grade for Exam 4:>");
int exam4 = scanner.nextShort();
while (exam4 > 20 || exam4 < 0) {
System.out.println("Error! Not within mark range!");
break;
}

int average = exam1 + exam2 + exam3 + exam4;
average /= 4;
System.out.println("You average grade is: " + average + "\n");

average *= 5;

System.out.println("Your score is" + average + "%!\n");

if (average > 70) {
System.out.println("You get an A overall");
} else if (average > 60 || average <= 70) {
System.out.println("You get a B overall");
} else if (average > 40 || average <= 60) {
System.out.println("You get a C overall");
} else {
System.out.println("You have failed the module!");
}


}
}

最佳答案

System.exit(0);

这可能就是您正在寻找的内容,而不是 break 语句。

关于java - 如果用户输入的整数不符合我的条件,如何结束控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40512965/

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