用Java定义一个学生类Student, 有属性name,no,age,sex等 * 2. 定义一个 学生管理类StudentManger , 用于存储全部学生 对象 的信息 * 它的行为有: * -添加学生:通过键盘录入学生信息,添加到集合中。 * -删除学生:通过键盘录入要删除学生的学号,将该学生对象从集合中删除。 * -修改学生:通过键盘录入要修改学生的学号,将该学生对象其他信息进行修改。 * -查看学生:将集合中的学生对象信息进行展示。 */
时间: 2023-06-18 10:07:38 浏览: 147
类的使用,学习,小结 java定义一个学生类
Student类的定义:
```java
public class Student {
private String name;
private String no;
private int age;
private String sex;
public Student(String name, String no, int age, String sex) {
this.name = name;
this.no = no;
this.age = age;
this.sex = sex;
}
// getter and setter methods
}
```
StudentManager类的定义:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
private ArrayList<Student> students = new ArrayList<>();
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生学号:");
String no = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 读取换行符
System.out.println("请输入学生性别:");
String sex = scanner.nextLine();
Student student = new Student(name, no, age, sex);
students.add(student);
System.out.println("添加成功!");
}
public void removeStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除学生的学号:");
String no = scanner.nextLine();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getNo().equals(no)) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("找不到该学生!");
}
public void modifyStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改学生的学号:");
String no = scanner.nextLine();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getNo().equals(no)) {
System.out.println("请输入新的学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入新的学生年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 读取换行符
System.out.println("请输入新的学生性别:");
String sex = scanner.nextLine();
students.get(i).setName(name);
students.get(i).setAge(age);
students.get(i).setSex(sex);
System.out.println("修改成功!");
return;
}
}
System.out.println("找不到该学生!");
}
public void showStudents() {
System.out.println("所有学生信息如下:");
for (Student student : students) {
System.out.println(student.getName() + "\t" + student.getNo() + "\t" + student.getAge() + "\t" + student.getSex());
}
}
}
```
使用示例:
```java
public class Main {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
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("0. 退出程序");
int choice = scanner.nextInt();
scanner.nextLine(); // 读取换行符
switch (choice) {
case 0:
System.exit(0);
break;
case 1:
manager.addStudent();
break;
case 2:
manager.removeStudent();
break;
case 3:
manager.modifyStudent();
break;
case 4:
manager.showStudents();
break;
default:
System.out.println("请输入正确的数字!");
break;
}
}
}
}
```
阅读全文