今天进行了测试 以下是我写的
package test;
public class student {
public String stunumber;
public String name;
public int age;
public boolean sex;
public double score;
public student() {
}
public student(String stunumber, String name, int age, boolean sex, double score) {
this.stunumber = stunumber;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
public String getStunumber() {
return stunumber;
}
public void setStunumber(String stunumber) {
this.stunumber = stunumber;
}
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 boolean getSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
package test;
import java.util.Scanner;
public class StudentManager {
private static student[] students = new student[5];
private static int studentCount;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("");
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("5、查询学生信息;");
System.out.println("6、退出系统;");
System.out.println("***************************************************************");
System.out.print("请输入选择的数字: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
showStudents();
break;
case 2:
System.out.print("请输入学号: ");
String stuNumber = scanner.next();
System.out.print("请输入姓名: ");
String name = scanner.next();
System.out.print("请输入年龄: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入性别(男: false, 女: true): ");
boolean sex = scanner.nextBoolean();
scanner.nextLine();
System.out.print("请输入成绩: ");
double score = scanner.nextDouble();
scanner.nextLine();
addStudent(stuNumber, name, age, sex, score);
break;
case 3:
System.out.print("请输入要删除的学号: ");
String delStuNumber = scanner.next();
deleteStudent(delStuNumber);
break;
case 4:
System.out.print("请输入要修改的学号: ");
String updateStuNumber = scanner.next();
System.out.print("请输入新姓名: ");
String newName = scanner.next();
System.out.print("请输入新年龄: ");
int newAge = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入新性别(男: false, 女: true): ");
boolean newSex = scanner.nextBoolean();
scanner.nextLine();
System.out.print("请输入新成绩: ");
double newScore = scanner.nextDouble();
scanner.nextLine();
updateStudent(updateStuNumber, newName, newAge, newSex, newScore);
break;
case 5:
System.out.print("请输入要查询的学号: ");
String queryStuNumber = scanner.next();
selectStudent(queryStuNumber);
break;
case 6:
System.out.println("退出系统。");
return;
default:
System.out.println("无效输入,请重新选择。");
break;
}
}
}
// 显示学生信息
public static void showStudents() {
for (int i = 0; i < studentCount; i++) {
System.out.println(students[i].getStunumber());
System.out.println(students[i].getName());
System.out.println(students[i].getAge());
if(students[i].getSex()){
System.out.println("女");
}else {
System.out.println("男");
}
System.out.println(students[i].getScore());
}
}
// 添加学生信息
public static void addStudent(String stuNumber, String name, int age, boolean sex, double score) {
int flag = -1;
for (int i = 0; i < studentCount; i++) {
if (students[i] != null && students[i].getStunumber().equals(stuNumber)) {
flag = i;
}
}
if (flag != -1) {
System.out.println("学号重复,无法添加新学生。");
return;
}
if (studentCount < students.length) {
students[studentCount++] = new student(stuNumber, name, age, sex, score);
System.out.println("学生添加成功。");
} else {
System.out.println("学生数量已满,无法添加新学生。");
}
}
// 删除学生信息
public static void deleteStudent(String stuNumber) {
int flag = -1;
for (int i = 0; i < studentCount; i++) {
if (students[i] != null && students[i].getStunumber().equals(stuNumber)) {
flag = i;
}
}
if (flag == -1) {
System.out.println("未找到该学号的学生。");
return;
} else {
students[flag] = null; // 清空该学生信息
System.out.println("学生信息已删除。");
}
}
// 更新学生信息
public static void updateStudent(String stuNumber, String name, int age, boolean sex, double score) {
int flag = -1;
for (int i = 0; i < studentCount; i++) {
if (students[i] != null && students[i].getStunumber().equals(stuNumber)) {
flag = i;
}
}
if (flag == -1) {
System.out.println("未找到该学号的学生。");
return;
} else {
students[flag].setName(name);
students[flag].setAge(age);
students[flag].setSex(sex);
students[flag].setScore(score);
System.out.println("学生信息已更新。");
}
}
// 查询学生信息
public static void selectStudent(String stuNumber) {
int flag = -1;
for (int i = 0; i < studentCount; i++) {
if (students[i] != null && students[i].getStunumber().equals(stuNumber)) {
flag = i;
}
}
if (flag == -1) {
System.out.println("未找到该学号的学生。");
return;
} else {
System.out.println(students[flag].getStunumber());
System.out.println(students[flag].getName());
System.out.println(students[flag].getAge());
if(students[flag].getSex()){
System.out.println("女");
}else {
System.out.println("男");
}
System.out.println(students[flag].getScore());
}
}
}
标签:day8,students,System,flag,println,public,out From: https://www.cnblogs.com/wjhfree/p/18422665