Swtich-case 与if -else 的转换
编写程序:从键盘上读入一个学生成绩,存放在变量score中,根据score的
值输出其对应的成绩等级:
score>=90 等级: A
70<=score<90 等级: B
60<=score<70 等级: C
score<60 等级: D
import java.util.Scanner;
class SwtichCaseExer1
{
public static void main(String[] args)
{
System.out.println("请您输入您的成绩");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score>=90)
{
System.out.println("您的等级是A");
}else if (score>=70&&score<90)
{
System.out.println("您的等级是B");
}else if (score>=60&&score<70)
{
System.out.println("您的等级是C");
}else if (score<60)
{
System.out.println("您的等级是D");
}else{
System.out.println("请输入正确的成绩");
}
}
}
代码运行截图

本程序通过读取用户输入的成绩,使用if-else条件语句判断并输出对应的等级:A(90分以上),B(70-89分),C(60-69分)或D(60分以下)。
1328

被折叠的 条评论
为什么被折叠?



