Java title base algorithm (05): using a nested conditional operator to complete this question: academic> = 90 points represented by students A, B is represented by between 60-89 minutes, 60 minutes or less is represented by C .

View all 50 basic arithmetic questions, see:

Java basic algorithm of 50 questions

package Demo05Grade;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Grade {
    /**
     * 利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
     */
    /*
    分析:首先,需要用户来通过Scanner类来输入一个成绩,并且我们要判断用户输入的合法性
         其次,我们需要对这个成绩进行判断,所以需要if...else语句
         最后,根据题目要求,我们还需要使用条件运算符的嵌套来完成该题
     */
    public static void main(String[] args) {
        while(true) {
            System.out.println("请输入一个成绩:");
            Scanner sc = new Scanner(System.in);
            int score = 0;
            try {
                score = sc.nextInt();
                // 对用户输入的成绩进行判断
                if(score>=90){
                    System.out.println("该分数的评级为:A ");
                }else if(score >=60 && score<=89){
                    System.out.println("该分数的评级为:B ");
                }else if(score>=0 && score<60){
                    System.out.println("该分数的评级为:C ");
                }else if(score<0){
                    System.out.println("分数不可以为负数,不予评级,请重新输入:");
                }
            } catch (InputMismatchException e) {
                System.out.println("您输入的成绩有误,请重新输入:");
            }
        }
    }
}
Published 22 original articles · won praise 1 · views 1427

Guess you like

Origin blog.csdn.net/weixin_44803446/article/details/105354724