分析考试结果限制(Java)
我是初学者,我正在做Deitel的练习如何编程我制作了一个程序来分析考试结果,并输出奖励给老师与否。我的程序运行良好,但我想,如果用户输入的值不是1或2,那么它应该给出一个错误,并终止程序
Following is the code:
package test;
import java.util.Scanner;
public class apple {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int failure = 0;
int passes = 0;
int counter = 1;
while (counter <= 10){
System.out.print("Enter Result (For Pass Enter 1 or for Fail Enter 2): ");
int result = input.nextInt();
if (result == 1)
passes = passes + 1;
else
failure = failure + 1;
counter = counter + 1;
}
System.out.printf("Total Pass Students are %d and Failed are %d" ,passes , failure );`enter code here`
if (passes > 8)
System.out.println("\nBonus to instructor");
}
}
# 1 楼答案
为其他号码抛出一个异常如何?您可能应该使用自己的非通用异常,所以请用合适的类扩展异常替换它