解题思路:本题属于条件判断类型的题目。首先可以想到的是if~else if ,我们既可以把条件设置为(<=xx&&>xx),再思考可以知道当我们设置了一个条件后,下面的条件就已经隐含了它的范围在上述条件之外,故可以把条件设置为 >=90、>=80(已经暗含该条件<90)、·······。然后可以想到是switch语句,只需要将输入的分数除以10,就可以将分数划分为各个分段,如80,82,89,······就会变成8.
注意事项:switch语句中如果每种情况不一样的话,就需要在语句末尾加上break,这个不要忘记了,否则程序就会依次往下执行
参考代码:
if~else if 代码:
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n>=90)
System.out.println('A');
else if(n>=80)
System.out.println('B');
else if (n>=70)
System.out.println('C');
else if (n>=60)
System.out.println('D');
else
System.out.println('E');
}
}
switch代码:
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int t=n/10;
switch(t)
{
case 10:;
case 9:System.out.println('A');break;
case 8:System.out.println('B');break;
case 7:System.out.println('C');break;
case 6:System.out.println('D');break;
default:System.out.println('E');
}
}
}
0.0分
0 人评分