条件运算符嵌套问题:成绩评级

来源:互联网 发布:apache maven安装 编辑:程序博客网 时间:2024/04/26 17:10
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。  


程序分析:(a>b)?a:b这是条件运算符的基本例子。


方法一:

public static void Score1(){int score;System.out.print("请输入成绩:");Scanner sc=new Scanner(System.in);score=sc.nextInt();String str=(score>89)?"等级:A":(score>59)?"等级:B":"等级:C"; //利用(a>b)?a:b判断成绩等级System.out.print(str);}



方法二:还可以用if else语句完成

public static void Score2(){int score;Scanner sc=new Scanner(System.in);score=sc.nextInt();//90分及以上为Aif(score>89) {System.out.print("A");}//60-89分为Belse if(score>59) {System.out.print("B");}//60分以下为Celse {System.out.print("C");}}


0 0
原创粉丝点击