要求用Java编写程序,系统要求输入成绩,用户输入成绩,系统输出成绩,要求分别用if else,Switch case 选择结构语句实现
时间: 2024-09-20 20:06:49 浏览: 44
当然可以。在Java中,你可以使用`if-else`和`switch-case`语句来根据用户的输入判断并输出成绩的情况。这里给出两个例子:
**使用 if-else 语句实现:**
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的成绩:");
int score = scanner.nextInt();
if (score >= 90) {
System.out.println("优秀:" + score);
} else if (score >= 60) {
System.out.println("及格:" + score);
} else {
System.out.println("不及格:" + score);
}
scanner.close();
}
}
```
在这个例子中,如果分数大于等于90,就认为是优秀;大于等于60但小于90则认为是及格;否则认为不及格。
**使用 switch-case 语句实现:**
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的成绩:");
int score = scanner.nextInt();
switch (score / 10) {
case 9:
System.out.println("优秀:" + score);
break;
case 6:
System.out.println("及格:" + score);
break;
default:
System.out.println("不及格:" + score);
}
scanner.close();
}
}
```
这里将分数分为九十分段,然后根据每段的结果输出相应的评价。
阅读全文
相关推荐


















