题目如下:
定义一个异常类ScoreException,当输入的学生成绩不在[0,100]区间时,抛出该异常。
定义一个异常类StudentNumberException,当输入的学号不满足下述条件,则抛出该异常。条件为:
学号为10位,第1位为2,第2位为0,其余位为数字0~9.
对Student和StudentTest类进行必要修改,提升程序的健壮性。
自己定义异常,但编译出现异常,不知如何修改
package assignment;
import java.util.Scanner;
public class StudentException {
public static void main (String args[]){
Scanner sc = new Scanner (System.in);
try {
String number = sc.next();
String name = sc.next();
int maths = sc.nextInt();
int english = sc.nextInt();
int science = sc.nextInt();
double average = (maths + english + science )*1.0/3.0;
System.out.println("Student ID:"+number);
System.out.println("Name:"+name);
System.out.println("Math:"+maths);
System.out.println("English:"+english);
System.out.println("Science:"+science);
System.out.printf("Average Score:"+"%.1f",average);
System.out.print("\n");
}catch(StudentNumberException e2) {
System.out.println(" Illegal number format ");
}
catch(ScoreException e1) {
System.out.println(" Illegal score format ");
}
}
}
class ScoreException extends Exception{
private int maths;
private int english;
private int science;
ScoreException(int maths,int english, int science ){
this.maths= maths;
this.english = english;
this.science = science;
}
void throwException(int maths,int english, int science) throws ScoreException{
if((maths<0||maths>100)||(english<0||english>100)||(science<0||science>100)) {
throw new ScoreException(maths, english, science);
}
}
}
class StudentNumberException extends Exception{
private String number;
StudentNumberException(String number){
this.number=number;
}
void throwsException(String number) throws StudentNumberException {
if(number.length()!=10||number.charAt(0)!='2'||number.charAt(1)!='1') {
throw new StudentNumberExceptio(number);
}
}
}
输入:
2011211301 Tom 88 79 90
输出:
throwException这个函数什么时候调用过了
你是不是不知道什么叫异常类,异常类是用来定义异常类型的,里面不要写奇怪的方法