PTA jmu-Java-06 Exception-04-Custom Exception (Comprehensive) Score 10

Define the IllegalScoreException exception class, which represents the exception that the sum of scores exceeds the reasonable range. The exception is a checked exception, that is, it is hoped that the exception must be caught and processed.

Define the IllegalNameException exception class, which represents the exception whose name is set unreasonably. The exception is unchecked exception

Define the Student class.

Attributes:

private String name;
private int score;

method:

toString          //自动生成
setter/getter     //自动生成
改造setName       //如果姓名首字母为数字则抛出IllegalNameException
public int addScore(int score)  //如果加分后分数<0 或>100,则抛出IllegalScoreException,加分不成功。

main method

  1. Enter new to create a new student object. Then enter a row of student data in the format of name and age, and then call setName, addScore. Otherwise break out of the loop.

  1. If setName is unsuccessful, an exception will be thrown, and the exception information will be printed, and then the processing of the next line will continue.

  1. If addScore is unsuccessful, an exception will be thrown, and the exception information will be printed, and then the processing of the next line will continue. If both 2 and 3 are successful, print the student information (toString)

  1. If any other exception occurs while parsing the row of student data, print the exception message and continue with the next row.

  1. Scanner is also a resource, and it is hoped that the program will be closed regardless of whether an exception is thrown or not. After closing, use System.out.println("scanner closed") to print the closing information

Note: Use System.out.println(e); to print exception information, e is the exception generated.

Input sample:

new
zhang 10
new
wang 101
new
wang30
new
3a 100
new
wang 50
other

Sample output:

Student [name=zhang, score=10]
IllegalScoreException: score out of range, score=101
java.util.NoSuchElementException
IllegalNameException: the first char of name must not be digit, name=3a
Student [name=wang, score=50]
scanner closed

Code length limit 16 KB

Time limit 400 ms

Memory limit 64 MB

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IllegalNameException, IllegalScoreException {
        Scanner sc = new Scanner(System.in);
        while (true) {
            String str = sc.next();
            int flag = 0;
            if (str.equals("new")) {
                String name = sc.next();
                int score = 0;
                try {
                    score = sc.nextInt();
                } catch (Exception e) {
                    System.out.println("java.util.NoSuchElementException");
                    continue;
                }
                Student student = new Student(name, score);
                flag += student.setName(name);
                flag += student.addScore(score);
                if(flag == 2) System.out.println(student.toString());
            } else {
                break;
            }
        }
        sc.close();
        System.out.println("scanner closed");
    }
}

class Student {

    private String name;

    private int score;

    public Student() {
    }

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int setName(String name) throws IllegalNameException {
        this.name = name;
        if(name.charAt(0)>'0' && name.charAt(0) < '9'){
            System.out.println("IllegalNameException: the first char of name must not be digit, name=" + name);
            return 0;
        }
        return 1;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student [" +
                "name=" + name +
                ", score=" + score +
                ']';
    }


    public int addScore(int score) throws IllegalScoreException {
        if (score < 0 || score > 100 ) {
            System.out.println("IllegalScoreException: score out of range, score=" + score);
            return 0;
        }
        return 1;
    }
}

class IllegalScoreException extends Exception{
    public IllegalScoreException() {
    }

    public IllegalScoreException(String message) {
        super(message);
    }
}

class IllegalNameException extends Exception{
    public IllegalNameException() {
    }

    public IllegalNameException(String message) {
        super(message);
    }
}

Guess you like

Origin blog.csdn.net/weixin_70206677/article/details/129343837