题目描述
在上次作业的学生类(1047 学生类)基础上:
定义一个异常类ScoreException,当输入的学生成绩不在[0,100]区间时,抛出该异常。
定义一个异常类StudentNumberException,当输入的学号不满足下述条件,则抛出该异常。条件为:
学号为10位,第1位为2,第2位为0,其余位为数字0~9.
对Student和StudentTest类进行必要修改,提升程序的健壮性。
输入
只有一行,先是2个字符串,依次代表number, name, 然后是三个整数,依次代表 maths, english, science 的成绩。
输出
如果成绩非法,则输出 Illegal score format ;如果学号非法则输出 Illegal number format 。
如果正常,则打印学生信息,平均分保留一位小数。具体格式见输出样例。
样例输入 Copy
2011211301 Tom 88 79 90
样例输出 Copy
Student ID:2011211301 Name:Tom Math:88 English:79 Science:90 Average Score:85.7
import java.text.DecimalFormat;
import java.text.Format;
import java.util.*;
public class StudentExceptionTest {
static void checkscore(int score1,int score2,int score3) throws ScoreException
{
if(score1<0||score1>100||score2<0||score2>100||score3<0||score3>100)
{
throw new ScoreException("Illegal score format");
}
}
static void checkStudentNumber(String num) throws StudentNumberException
{
int flag=0;
if(num.length()==10)
{
flag=0;
}
else
{
flag=1;
}
if(num.charAt(0)!='2')
{
flag=1;
}
if(num.charAt(1)!='0')
{
flag=1;
}
for(int i=2;i<=num.length()-1;i++)
{
if(num.charAt(i)<'0'||num.charAt(i)>'9')
{
flag=1;
}
}
if(flag==1){
throw new StudentNumberException("Illegal number format");
}
}
public static void main(String[] args) throws ScoreException {
Scanner sc = new Scanner(System.in);
Student it=new Student();
String number = sc.next();
try{
checkStudentNumber(number);
}
catch (StudentNumberException e){
System.out.println(e.getMessage());
return;
}
String name = sc.next();
it.student(number, name);
int math,english,science;
math= sc.nextInt();
english= sc.nextInt();
science= sc.nextInt();
try{
checkscore(math,english,science);
}
catch (ScoreException e){
System.out.println(e.getMessage());
return ;
}
it.enterMarks(math,english, science);
it.calculateAverage();
System.out.println(it.toString());
}
}
class Student{
String studentNumber;
String studentName;
int markForMaths;
int markForEnglish;
int markForScience;
public void student(String number, String name)
{
studentNumber=number;
studentName=name;
}
public Student()
{
}
public String getNumber()
{
return studentNumber;
}
public String getName()
{
return studentName;
}
public void enterMarks(int markForMaths, int markForEnglish, int markForScience)
{
this.markForMaths=markForMaths;
this.markForEnglish=markForEnglish;
this.markForScience=markForScience;
}
public int getMathsMark()
{
return markForMaths;
}
public int getEnglishMark()
{
return markForEnglish;
}
public int getScienceMark()
{
return markForScience;
}
public double calculateAverage()
{
return (markForMaths+markForEnglish+markForScience)/3.0;
}
public String toString()
{
Format df=new DecimalFormat("#0.0");
return "Student ID:"+studentNumber+"\n"+"Name:"+studentName+"\n"+"Math:"+markForMaths+"\n"+"English:"+markForEnglish+"\n"+"Science:"+markForScience+"\n"+"Average Score:"+df.format(this.calculateAverage());
}
}
class ScoreException extends Exception {
public ScoreException(String error){
super(error);
}
}
class StudentNumberException extends Exception {
public StudentNumberException(String error){
super(error);
}
}
Java实现学号与成绩异常处理
文章描述了一个Java程序,该程序在学生类的基础上增加了两个异常类:ScoreException用于处理分数不在0-100范围的情况,StudentNumberException处理学号格式不正确的问题。程序通过检查函数确保输入的学生成绩和学号合法,以提高程序的健壮性。
1406





