Java第二次测试代码:
public class Student {
String stunumber;
String name;
int age;
boolean sex;
double score;
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 char isFM() {
if (sex)
return 'M';
else
return 'F';
}
public String getStunumber() {
return stunumber;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public boolean isSex() {
return sex;
}
public double getScore() {
return score;
}
public void setStunumber(String stunumber) {
this.stunumber = stunumber;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public void setScore(double score) {
this.score = score;
}
}
import java.util.Objects;
public class StudentManager {
private Student[] students;
public int index;
public StudentManager(){
students=new Student[10];
index=0;
}
public void showStudent(){
for(int i=0;i<index;i++) {
System.out.println(students[i].getStunumber()+"\t"+students[i].getName()+"\t"+students[i].getAge()+"\t"+students[i].isFM()+"\t"+students[i].getScore());
}
}
public void addStudent(String stunumber,String name,int age,boolean sex,double score){
for(int i=0;i<index;i++){
if(Objects.equals(stunumber, students[i].getStunumber())){
System.out.println("学号重复,请重新选择!");
return;
}
}
students[index]=new Student(stunumber,name,age,sex,score);
index++;
}
public void deleteStudent(String stunumber){
int flag=0;
for(int i=0;i<index;i++){
if(Objects.equals(stunumber, students[i].getStunumber())){
flag=1;
for(int j=i;j<index;j++){
if(j<index-1){
students[j].setStunumber(students[j+1].getStunumber());
students[j].setName(students[j=1].getName());
students[j].setSex(students[j+1].isSex());
students[j].setAge(students[j+1].getAge());
students[j].setScore(students[j+1].getScore());
}
else if(j==index-1){
students[j].setStunumber(null);
students[j].setName(null);
students[j].setSex(true);
students[j].setAge(0);
students[j].setScore(0);
}
}
index--;
}
}
if(flag==0)
System.out.println("学号不存在,请重新选择!");
}
public void updateStudent(String stunumber,String name,int age,boolean sex,double score){
int flag=0;
for(int i=0;i<index;i++){
if(Objects.equals(stunumber, students[i].getStunumber())){
students[i].setStunumber(stunumber);
students[i].setName(name);
students[i].setAge(age);
students[i].setSex(sex);
students[i].setScore(score);
flag=1;
}
}
if(flag==0)
System.out.println("学号不存在,请重新选择!");
}
public void selectStudent(String stunumber){
int flag=0;
for(int i=0;i<index;i++){
if(Objects.equals(stunumber, students[i].getStunumber())){
flag=1;
System.out.println(students[i].getStunumber()+"\t"+students[i].getName()+"\t"+students[i].getAge()+"\t"+students[i].isFM()+"\t"+students[i].getScore());
}
}
if(flag==0)
System.out.println("学号不存在,请重新选择!");
}
public void menu(){
System.out.println("***************************************************************");
System.out.println("石家庄铁道大学软件工程系学生信息管理系统");
System.out.println("***************************************************************");
System.out.println("1、\t遍历输出学生信息:");
System.out.println("2、\t新学生信息录入:");
System.out.println("3、\t删除学生信息:");
System.out.println("4、\t修改学生信息:");
System.out.println("5、\t查询学生信息:");
System.out.println("***************************************************************");
System.out.println("请选择:");
}
}
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String stunumber;String name;int age;boolean sex;double score;
StudentManager manager=new StudentManager();
while (true){
int choice;
manager.menu();
choice=sc.nextInt();
switch (choice){
case 1:
manager.showStudent();
break;
case 2:
stunumber=sc.next();
name=sc.next();
age=sc.nextInt();
sex=sc.nextBoolean();
score=sc.nextDouble();
manager.addStudent(stunumber,name,age,sex,score);
break;
case 3:
stunumber=sc.next();
manager.deleteStudent(stunumber);
break;
case 4:
stunumber=sc.next();
name=sc.next();
age=sc.nextInt();
sex=sc.nextBoolean();
score=sc.nextDouble();
manager.updateStudent(stunumber,name,age,sex,score);
break;
case 5:
stunumber=sc.next();
manager.selectStudent(stunumber);
break;
default:
return;
}
}
}
}