switch (true){ case (score>90): System.out.println("爸爸买辆车。"); break; case (score<=90&&score>80): System.out.println("妈妈买电脑"); break; System.out.println("妈给买电脑"); case (score<=80&&score>60): System.out.println("奶奶给买手机"); break; default: System.out.println("没有礼物"); break; }
时间: 2024-02-14 15:27:13 浏览: 41
这段代码是一个用来根据分数来决定给予什么礼物的逻辑判断。根据给定的分数,代码会根据不同的条件输出不同的结果。
但是这段代码有一些问题。首先,`switch` 语句的 `case` 表达式应该是常量表达式,而不是条件表达式。所以 `case (score>90)` 和 `case (score<=90&&score>80)` 是不合法的。
另外,在第二个 `case` 语句块后面的 `System.out.println("妈给买电脑");` 是无效的,因为它在 `break` 语句之后。应该将这行代码移到正确的位置。
修正后的代码如下:
```java
if (score > 90) {
System.out.println("爸爸买辆车。");
} else if (score <= 90 && score > 80) {
System.out.println("妈妈买电脑");
System.out.println("妈给买电脑");
} else if (score <= 80 && score > 60) {
System.out.println("奶奶给买手机");
} else {
System.out.println("没有礼物");
}
```
这样根据分数的不同,就会输出对应的结果。
相关问题
逐行解释下面代码:package sec02; import java.io.*; import java.util.*; class Student implements Serializable { private String name; private int id; private int chineseScore; private int mathScore; private int englishScore; public Student(String name, int id) { this.name = name; this.id = id; } public void setChineseScore(int score) { this.chineseScore = score; } public void setMathScore(int score) { this.mathScore = score; } public void setEnglishScore(int score) { this.englishScore = score; } public String getName() { return name; } public int getId() { return id; } public int getChineseScore() { return chineseScore; } public int getMathScore() { return mathScore; } public int getEnglishScore() { return englishScore; } public String setName(String name) { return name; } } public class StudentSystem { private static final String FILE_NAME = "students.dat"; private static List<Student> students = new ArrayList<>(); public static void main(String[] args) { loadStudents(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加学生"); System.out.println("2. 查找学生"); System.out.println("3. 更新学生信息"); System.out.println("4. 删除学生"); System.out.println("5. 输入学生成绩"); System.out.println("6. 退出系统"); int choice = scanner.nextInt(); switch (choice) { case 1: addStudent();//添加 break; case 2: findStudent();//寻找 break; case 3: updateStudent();//更新 break; case 4: deleteStudent();//删除
这段代码是一个简单的学生管理系统,使用了Java的序列化机制来保存和读取学生信息。
首先,代码中使用了Java的输入输出包和工具包:
```java
package sec02;
import java.io.*;
import java.util.*;
```
然后定义了一个学生类`Student`,实现了序列化接口`Serializable`,并有一些属性和方法:
```java
class Student implements Serializable {
private String name;
private int id;
private int chineseScore;
private int mathScore;
private int englishScore;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public void setChineseScore(int score) {
this.chineseScore = score;
}
public void setMathScore(int score) {
this.mathScore = score;
}
public void setEnglishScore(int score) {
this.englishScore = score;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getChineseScore() {
return chineseScore;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public String setName(String name) {
return name;
}
}
```
接下来是主类`StudentSystem`,其中定义了一个常量`FILE_NAME`,用于指定保存学生信息的文件名。还有一个`List`类型的变量`students`,用于存放所有学生信息。程序中的主要逻辑在`main`方法中,通过一个`while`循环来不断接收用户的操作:
```java
public class StudentSystem {
private static final String FILE_NAME = "students.dat";
private static List<Student> students = new ArrayList<>();
public static void main(String[] args) {
loadStudents();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加学生");
System.out.println("2. 查找学生");
System.out.println("3. 更新学生信息");
System.out.println("4. 删除学生");
System.out.println("5. 输入学生成绩");
System.out.println("6. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
findStudent();
break;
case 3:
updateStudent();
break;
case 4:
deleteStudent();
break;
case 5:
inputScore();
break;
case 6:
saveStudents();
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入。");
break;
}
}
}
```
其中,`loadStudents`方法用于从文件中读取学生信息,`saveStudents`方法用于将学生信息保存到文件中,`addStudent`方法用于添加一个新学生,`findStudent`方法用于查找学生信息,`updateStudent`方法用于更新学生信息,`deleteStudent`方法用于删除学生信息,`inputScore`方法用于输入学生成绩。
最后,程序中的各个方法实现了相应的功能,有些方法使用了Java的序列化机制来保存和读取学生信息,具体实现可以参考完整代码。
帮我写一个java学生成绩管理面板 ,要求有学生类(ZSStudent),成绩类(ZSGrade),管理类(ZSSrudentManagerSystem),程序运行入口:ZSStudentManagerSystem.run();
好的,我可以帮您编写一个Java学生成绩管理面板。以下是代码示例:
ZSStudent.java:
```java
public class ZSStudent {
private String name;
private int age;
private String id;
public ZSStudent(String name, int age, String id) {
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
```
ZSGrade.java:
```java
import java.util.HashMap;
public class ZSGrade {
private HashMap<String, Double> grades;
public ZSGrade(HashMap<String, Double> grades) {
this.grades = grades;
}
public Double getGrade(String courseId) {
if (grades.containsKey(courseId)) {
return grades.get(courseId);
} else {
return 0.0;
}
}
public void setGrade(String courseId, Double score) {
grades.put(courseId, score);
}
public double getAverage() {
double sum = 0.0;
for (Double score : grades.values()) {
sum += score;
}
return sum / grades.size();
}
}
```
ZSStudentManagerSystem.java:
```java
import java.util.HashMap;
import java.util.Scanner;
public class ZSStudentManagerSystem {
private static HashMap<String, ZSGrade> gradeMap = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);
public static void run() {
while (true) {
printMenu(); // 打印菜单
int option = scanner.nextInt();
switch (option) {
case 1:
addNewStudent(); // 添加学生
break;
case 2:
addNewGrade(); // 添加成绩
break;
case 3:
printGrade(); // 显示成绩
break;
case 4:
System.out.println("谢谢使用,再见!");
return;
default:
System.out.println("输入错误,请重新选择!");
}
}
}
private static void printMenu() {
System.out.println("学生成绩管理系统");
System.out.println("-----------------------");
System.out.println("1. 添加学生");
System.out.println("2. 添加成绩");
System.out.println("3. 显示成绩");
System.out.println("4. 退出");
System.out.println("-----------------------");
System.out.println("请选择功能:");
}
private static void addNewStudent() {
System.out.println("请输入新学生的姓名:");
String name = scanner.next();
System.out.println("请输入新学生的年龄:");
int age = scanner.nextInt();
System.out.println("请输入新学生的学号:");
String id = scanner.next();
ZSStudent student = new ZSStudent(name, age, id);
gradeMap.put(id, new ZSGrade(new HashMap<String, Double>()));
System.out.println("添加学生成功!");
}
private static void addNewGrade() {
System.out.println("请输入成绩所属学生的学号:");
String studentId = scanner.next();
if (!gradeMap.containsKey(studentId)) {
System.out.println("该学生不存在!");
return;
}
System.out.println("请输入课程名称:");
String courseId = scanner.next();
System.out.println("请输入成绩:");
double score = scanner.nextDouble();
ZSGrade grade = gradeMap.get(studentId);
grade.setGrade(courseId, score);
System.out.println("添加成绩成功!");
}
private static void printGrade() {
System.out.println("请输入要查询的学生学号:");
String studentId = scanner.next();
if (!gradeMap.containsKey(studentId)) {
System.out.println("该学生不存在!");
return;
}
ZSGrade grade = gradeMap.get(studentId);
System.out.println("学生姓名:" + grade.getName());
System.out.println("学生年龄:" + grade.getAge());
System.out.println("学生学号:" + grade.getId());
System.out.println("-----------------------");
System.out.println("| 课程名称\t| 成绩\t|");
System.out.println("-----------------------");
for (String courseId : grade.getCourseIds()) {
System.out.println("| " + courseId + "\t\t| " + grade.getGrade(courseId) + "\t|");
}
System.out.println("-----------------------");
System.out.println("平均分:" + grade.getAverage());
}
}
```
使用时,可以通过调用 `ZSStudentManagerSystem.run()` 运行程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)