解题思路:本题属于条件判断类型的题目。首先可以想到的是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 人评分
C二级辅导-分段函数 (C语言代码)浏览:577 |
printf基础练习2 (C语言代码)浏览:603 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:599 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:587 |
WU-链表数据求和操作 (C++代码)浏览:1366 |
IP判断 (C语言描述,蓝桥杯)浏览:1108 |
sizeof的大作用 (C语言代码)浏览:1566 |
A+B for Input-Output Practice (I) (C语言代码)浏览:448 |
简单的a+b (C语言代码)浏览:525 |
【魔板】 (C++代码)浏览:1223 |