用switch语句实现:用户输入一个学生成绩,按照五级制输入这个学生对应的等级。
时间: 2023-08-31 11:38:13 浏览: 111
### 回答1:
以下是使用switch语句实现的代码:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入学生成绩:");
int score = input.nextInt();
int level = score / 10; // 计算分数所在的等级
switch (level) {
case 10:
case 9:
System.out.println("该学生的等级为:优秀");
break;
case 8:
System.out.println("该学生的等级为:良好");
break;
case 7:
System.out.println("该学生的等级为:中等");
break;
case 6:
System.out.println("该学生的等级为:及格");
break;
default:
System.out.println("该学生的等级为:不及格");
break;
}
}
}
```
在这个代码中,我们使用了Scanner类来获取用户输入的学生成绩。然后,我们将分数除以10,以确定学生所在的等级。最后,我们使用switch语句来根据等级输出相应的结果。注意,我们在case语句中使用了多个值,这是因为分数在90分以上和100分时都应该输出“优秀”。
### 回答2:
可以使用switch语句来根据学生成绩进行等级的划分,并输出对应等级。下面是一个使用switch语句实现的例子:
```java
import java.util.Scanner;
public class StudentGrade {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生成绩:");
int score = scanner.nextInt();
scanner.close();
int grade = score / 20; //将成绩按照20分一个等级进行划分
switch (grade) {
case 0:
System.out.println("该学生的等级是:E");
break;
case 1:
System.out.println("该学生的等级是:D");
break;
case 2:
System.out.println("该学生的等级是:C");
break;
case 3:
System.out.println("该学生的等级是:B");
break;
case 4:
System.out.println("该学生的等级是:A");
break;
default:
System.out.println("输入的成绩无效!");
}
}
}
```
用户输入一个学生成绩后,程序会根据该成绩进行等级划分,并输出对应的等级。例如,如果用户输入的成绩为74,则根据五级制,成绩在60-79之间,对应的等级为C,因此程序会输出"该学生的等级是:C"。
阅读全文
相关推荐


















