PTA 成绩录入时的及格与不及格人数统计分数 10


200
69
30

输出样例:

在这里给出相应的输出。例如:

200invalid!
1
1

代码长度限制16 KB

时间限制6000 ms

内存限制64 MB

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a = 0, b = 0;
        for (int i = 0; i < n; i++) {
            score score = new score();
            try {
                score.setScore(sc.nextInt());
                int val = score.getScore();
                if (val >= 60) a++;
                else b++;
            } catch (IllegalScoreException e) {
                i--;
                System.out.println(score.toString());
            }
        }
        System.out.println(a);
        System.out.println(b);
    }
}

class score {
    private int score;

    public score() {
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) throws IllegalScoreException {
        this.score = score;
        if (score < 0 || score > 100) {
            throw new IllegalScoreException();
        }
    }

    @Override
    public String toString() {
        return score + "invalid!";
    }
}

class IllegalScoreException extends Exception {
}
PTA 成绩录入时的及格与不及格人数统计分数 10_第1张图片

你可能感兴趣的:(PTA,_Java,java)