添加一个 Student 类,定义属性
public class Student {
String name; //学生姓名
int studentNo; //学号
String Sex; //性别
String gradeName; //年级
int phone; //电话号码
定义一个 StudentTest 类进行操作
import java.util.Scanner;
public class StudentTest {
Student[] stus=new Student[30];
/**
* 初始化
*/
public void Initial(){
Student stu=new Student();
stu.name="小明";
stu.phone=12345;
stu.gradeName="S1";
stu.Sex="男";
stu.studentNo=66666;
stus[0]=stu;
}
/**
* 添加信息
*
*/
public boolean AddStudent(Student student){
for (int i=0;i<stus.length;i++){
if (stus[i]==null){
stus[i]=student;
return true;
}
}
return false;
}
/**
* 查询信息
*/
public void SearchStudent(){
System.out.println("姓名\t学号\t性别\t年级\t电话号码");
for (int i=0;i<stus.length;i++){
System.out.println(stus[i].name+"\t"+stus[i].studentNo+"\t"+
stus[i].Sex+"\t"+stus[i].gradeName+"\t"+stus[i].phone);
}
}
/**
* 删除信息
*/
public boolean DeleteStudent(int no){
for (int i=0;i<stus.length;i++){
if(stus[i]!=null){
if(stus[i].studentNo==no){
stus[i]=null;
return true;
}
}
}
return false;
}
public void ShowMeun(){
Initial();
String answer="";
do {
Scanner input=new Scanner(System.in);
System.out.println("1,查看信息");
System.out.println("2,新增信息");
System.out.println("3,删除信息");
System.out.println("请输入:");
int num=input.nextInt();
switch (num){
case 1:
SearchStudent();
case 2:
Student student=new Student();
System.out.println("请输入学号:");
student.studentNo=input.nextInt();
System.out.println("请输入姓名:");
student.name=input.next();
System.out.println("请输入性别:");
student.Sex=input.next();
System.out.println("请输入年级:");
student.gradeName=input.next();
System.out.println("请输入电话号码:");
student.phone=input.nextInt();
boolean isAdd=AddStudent(student);
if (isAdd){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
break;
case 3:
System.out.println("请输入要删除的学生学号:");
int no=input.nextInt();
boolean isDele=DeleteStudent(no);
if(isDele){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
break;
}
System.out.println("还要继续吗?(y/n)");
answer=input.next();
}while("y".equals(answer));
}
}
在main方法中调用
public class StudentInitial {
public static void main(String[] args) {
StudentTest student=new StudentTest();
student.ShowMeun();
}