北邮java oj 1051问题 A: 学生类(异常)

题目描述

在上次作业的学生类(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);

  }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值