9.20
今天进行了java的二次测试,了解了java里基本的增删改查的功能;以及查找时报错等细节处理
student类
package qixun;
public class student {
private String stunumber;
private String name;
private int age;
private boolean sex;
private 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 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;
}
}
studentManage类
点击查看代码
package qixun;
import java.util.Scanner;
public class studentManage {
public void Header(){
System.out.println("***************************************************************\n");
System.out.println("石家庄铁道大学软件工程系学生信息管理系统\n");
System.out.println("****************************************************************\n");
}
public void menu(){
System.out.println("1、\t遍历输出学生信息;\n" +
"2、\t新学生信息录入;\n" +
"3、\t删除学生信息;\n" +
"4、\t修改学生信息;\n" +
"5、\t查询学生信息;\n" +
"****************************************************************");
}
private student[] stds = new student[5];
private int count=0;
public void Menugo() {
Scanner scanner = new Scanner(System.in);
Header();
while (true){
menu();
System.out.println("请选择:");
int choice = scanner.nextInt();
switch (choice){
case 1:
showStdent();
break;
case 2:
addStudent();
break;
case 3:
deleteStudent();
break;
case 4:
updateStudent();
break;
case 5:
selectStudent();
break;
}
}
}
private void showStdent(){
for(int i=0;i<count;i++){
System.out.println("学号:"+stds[i].getStunumber());
System.out.println("姓名:"+stds[i].getName());
System.out.println("年零:"+stds[i].getAge());
System.out.println("性别:"+stds[i].getsex());
System.out.println("成绩:"+stds[i].getScore());
System.out.println("****************************************************************\n");
}
}
private void addStudent(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
String stunumber = scanner.nextLine();
for(int i=0;i<count;i++) {
if (stds[i].getStunumber().equals(stunumber)) {
System.out.println("学号重复");
}
}
System.out.println("请输入姓名:");
String name = scanner.nextLine();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
System.out.println("请输入性别:");
boolean sex = scanner.nextBoolean();
System.out.println("请输入成绩:");
double score = scanner.nextDouble();
student newstudent=new student(stunumber,name,age,sex,score);
stds[count]=newstudent;
count++;
}
private void deleteStudent(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
String stunumber = scanner.nextLine();
boolean flag=false;
for(int i=0;i<count;i++) {
if(stds[i].getStunumber().equals(stunumber)) {
for(int j=i;j+1<count;j++){
stds[j].setStunumber(stds[j+1].getStunumber());
stds[j].setName(stds[j+1].getName());
stds[j].setAge(stds[j+1].getAge());
stds[j].setSex(stds[j+1].getsex());
stds[j].setScore(stds[j+1].getScore());
}
count--;
flag=true;
}
}
if(flag==false){
System.out.println("未找到");
}
}
private void updateStudent(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
String stunumber = scanner.nextLine();
boolean flag=false;
for(int i=0;i<count;i++){
if(stds[i].getStunumber().equals(stunumber)){
System.out.println("请选择修改内容:\n" +
"1.学号 2.姓名 3.年龄 4.性别 5.成绩");
int choice1 = scanner.nextInt();
switch (choice1){
case 1:
System.out.println("请输入学号:");
String stunumber1 = scanner.nextLine();
stds[i].setStunumber(stunumber1);
break;
case 2:
System.out.println("请输入姓名:");
String name = scanner.nextLine();
stds[i].setName(name);
break;
case 3:
System.out.println("请输入年龄:");
int age = scanner.nextInt();
stds[i].setAge(age);
break;
case 4:
System.out.println("请输入性别:");
boolean sex = scanner.nextBoolean();
stds[i].setSex(sex);
break;
case 5:
System.out.println("请输入成绩:");
double score = scanner.nextDouble();
stds[i].setScore(score);
break;
}
flag=true;
}
}
if(flag==false){
System.out.println("未找到");
}
}
private void selectStudent(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
String stunumber = scanner.nextLine();
for(int i=0;i<count;i++) {
if(stds[i].getStunumber().equals(stunumber)){
System.out.println("学号:"+stds[i].getStunumber());
System.out.println("姓名:"+stds[i].getName());
System.out.println("年零:"+stds[i].getAge());
System.out.println("性别:"+stds[i].getsex());
System.out.println("成绩:"+stds[i].getScore());
System.out.println("****************************************************************\n");
}
}
}
public static void main(String[] args) {
studentManage management = new studentManage();
management.Menugo();
}
}