1、学生类
package ShuZu;
public class Studentt {
private String name ;
private String gender;
private int age;
private int id;
private double score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Studentt(){
}
public Studentt(String name,String gender,int age,int id,double score){
this.name = name;
this.gender = gender;
this.age = age;
this.id = id;
this.score = score;
}
}
2、方法类
package ShuZu;
import java.util.Arrays;
import java.util.Scanner;
public class StudentOperate {
Studentt[] arr = new Studentt[5];
public void init(){
arr[0] = new Studentt("张一","男",21,1001,90);
arr[1] = new Studentt("张二","男",20,1002,70);
arr[2] = new Studentt("张三","男",19,1003,90);
arr[3] = new Studentt("张四","男",21,1004,60);
arr[4] = new Studentt("张五","男",18,1005,90);
}
public void show(){
for(int i = 0;i < arr.length;i++){
System.out.println(arr[i].getId() + "\t" + arr[i].getName()+ "\t" +"\t"+ arr[i].getAge()+ "\t"+ arr[i].getGender()+ "\t" + arr[i].getScore());
}
}
public int findIndex(int id){
int index = -1;
for(int i = 0;i<arr.length;i++){
if(arr[i].getId() == id){
index =i;
break;
}
}
return index;
}
public void add(){
System.out.println("请输入你需要添加的学号");
Scanner input = new Scanner(System.in);
int id = input.nextInt();
int index = findIndex(id);
if(index == -1){
Studentt[] near = Arrays.copyOf(arr,arr.length+1);
System.out.println("请依次输入姓名、性别、年龄和分数");
near[arr.length] = new Studentt(input.next(),input.next(),input.nextInt(), id, input.nextDouble());
arr = near;
}else{
System.out.println("学号已存在");
}
}
public void update(){
System.out.println("请输入你需要修改的学号");
Scanner input = new Scanner(System.in);
int index = findIndex(input.nextInt());
if(index == -1){
System.out.println("学号不存在,请核实后重新输入");
}else{
System.out.println("请输入新的名字");
arr[index].setName(input.next());
System.out.println("请输入对应的性别");
arr[index].setGender(input.next());
System.out.println("请输入对应的年龄");
arr[index].setAge(input.nextInt());
System.out.println("请输入对应的分数");
arr[index].setScore(input.nextDouble());
}
}
public void delete(){
System.out.println("请输入你需要删除的学号");
Scanner input = new Scanner(System.in);
int index = findIndex(input.nextInt());
if(index == -1){
System.out.println("学号不存在,核实后重新输入");
}else{
Studentt[] near = new Studentt[arr.length-1];
System.arraycopy(arr,0,near,0,index);
System.arraycopy(arr,index+1,near,index,near.length-index);
arr = near;
}
}
}
3、测试
package ShuZu;
import java.util.Scanner;
public class StudenttTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
StudentOperate operate = new StudentOperate();
operate.init();
while(true){
System.out.println("1.查看\t2.添加\t3.修改\t4.删除\t0.退出");
int chose = input.nextInt();
switch(chose){
case 1:
operate.show();
break;
case 2:
operate.add();
break;
case 3:
operate.update();
break;
case 4:
operate.delete();
break;
case 0:
System.out.println("谢谢使用");
return;
default:
System.out.println("选择有误你核实");
break;
}
}
}
}
4、查看操作
2、添加操作
3、修改
4、删除