本编码答案为本人个人编辑,仅供参考。如有更优答案或者代码编写规范等问题欢迎读者私信本人或在下方评论处与本人交流。
1. 编写程序接收用户输入分数信息,如果分数在0—100之间,输出成绩。如果成绩不在该范围内,抛出异常信息,提示分数必须在0—100之间。
要求:使用自定义异常实现。
import java.util.Scanner;
public class ScoreException01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“请问您需要输入分数的个数:”);
int num = input.nextInt();
Student[] s = new Student[num];
for (int i = 0; i < s.length; i++) {
s[i] = new Student();
}
for (int i = 0, j = 0; j < s.length; i++) {
System.out.println("请输入分数:");
int score = input.nextInt();
try {
s[j++].setScore(score);
} catch (ScoreInputException e) {
e.printStackTrace();
j--;// 用于去除因为异常而未输入的数组中的空位
continue;
}
}
inpu