您的位置:首页 > 编程语言 > Java开发

北邮JAVA高级语言程序设计(选修课)第三次OJ作业

2020-07-18 05:02 609 查看

北邮JAVA高级语言程序设计(选修课)第三次OJ作业

题目描述:
在实验1基础上,
定义一个异常类ScoreException,当输入的学生成绩不在[0,100]区间时,抛出该异常。
定义一个异常类StudentNumberException,当输入的学号不满足下述条件,则抛出该异常。条件为:学号为10位,第1位为2,第2位为0,其余位为数字0~9.
对Student和StudentTest类进行必要修改,提升程序的健壮性。

StudentTest类运行效果如下:
测试用例1:

1011211301 WangXiao 85 75 95
Illegal number format

测试用例2:

2011211301 WangXiao 859 75 95
Illegal score format

测试用例3:

2011211301 WangXiao 85 75 95
Student Info:
Number:2011211301
Name:WangXiao
Math:85
English:75
Science:95
Ave:85.00`

源代码:

import java.util.Scanner;
import java.text.DecimalFormat;

public class StudentError
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
int judge = 1;//判断输入信息是否有误
int A = 1;//判断学号3~10位是否有误
int B = 1;//判断学号是否有误
int C = 1;//判断成绩是否有误
String line = in.nextLine();
String list[] = line.split(" ");
char[] array = list[0].toCharArray();

try
{

for(int i = 2;i < array.length;i++)
{
int INT = array[i] - 48;
if(INT < 0 || INT > 9)
{
A = 0;
break;
}
}
if(array.length != 10 || array[0] != '2' || array[1] != '0' || A == 0)
{
judge = 0;
B = 0;
}
int a1 = Integer.parseInt(list[2]);
int a2 = Integer.parseInt(list[3]);
int a3 = Integer.parseInt(list[4]);

if(a1 < 0 || a1 >100 || a2 < 0 || a2 > 100 || a3 < 0 || a3 > 100)
{
judge = 0;
C = 0;
}
if(judge == 0 && (A == 0 || B == 0))
{
throw new StudentNumber();
}
if(judge == 0 && C == 0)
{
throw new Score();
}
if(judge == 1)
{
double Average = (a1 + a2 + a3)/3.0;
DecimalFormat a = new DecimalFormat("#.00");
System.out.println("Student Info:"+"\n"+"Number:"+list[0]+"\n"+"Name:"+list[1]+
"\n"+"Math:"+list[2]+"\n"+"English:"+list[3]+"\n"+"Science:"+
list[4]+"\n"+"Ave:"+a.format(Average));
}
}
catch (Exception e)
{

}
}
}

}

class StudentNumber extends Exception
{
public StudentNumber()
{
System.out.println("Illegal number format");
}
}
class Score extends Exception
{
public Score()
{
System.out.println("Illegal score format");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: