题目

读入 0~100 的整数分数,输出等级:90~100 为 A,80~89 为 B,60~79 为 C,低于 60 为 D。

输入格式:一行输入一个整数,表示考试成绩。

输出格式:输出单个大写英文字母等级(A、B、C 或 D),末尾换行。

数据范围与约定:0 ≤ 分数 ≤ 100。

思路与算法

1. 多分支判断的顺序性
使用 if - else if - else 阶梯结构时,分支条件应当按数值从大到小(或从小到大)严格有序排列。先判定 score >= 90,后续分支自动隐含了 score < 90 的前提。

2. 边界值的归属
题意明确 90~100 为 A、80~89 为 B、60~79 为 C、低于 60 为 D。比较符号需严格采用 >= 90、>= 80、>= 60,避免误写导致 80 分、90 分落入下层区间。

完整程序

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int score = sc.nextInt();

        // 分支从高到低依次判定,后续分支自动隐含了小于上一档区间的前提
        if (score >= 90) {
            System.out.println("A");
        } else if (score >= 80) {
            System.out.println("B");
        } else if (score >= 60) {
            System.out.println("C");
        } else {
            System.out.println("D");
        }
    }
}

运行示例

输入:

85

输出:

B