用数组存放对象,然后遍历数组,加上一些数组的操作,很简单,纯属记录
import java.util.Scanner;
public class StudentManagerTwice {
public static void main(String[] args) {
Student1 stu = new Student1("aaa",12,1201);
Student1 stu2 = new Student1("bbb",34,1202);
Student1 stu3 = new Student1("ccc",14,1203);
Student1 stu4 = new Student1("ddd",16,1204);
Student1 stu5 = new Student1("eee",11,1204);
Student1[] stus = new Student1[5];
stus[0] = stu;
stus[1] = stu2;
stus[2] = stu3;
stus[3] = stu4;
stus[4] = stu5;
String answer = "";
Scanner input = new Scanner(System.in);
System.out.println("-----welcomecome to the students manager system-----");
do {
System.out.println(" 1.look all 2.query stuNum ");
System.out.println(" 3.exit 4.without ");
int choice = input.nextInt();
switch (choice) {
case 1:
for (int i = 0; i < stus.length; i++) {
System.out.println(stus[i]);
}
break;
case 2:
System.out.println("please input a stuNum: ");
int num = input.nextInt();
for (int i = 0; i < stus.length; i++) {
Student1 s = stus[i];
if(num == s.getStuNum()){
System.out.println(s);
break; //这里用break提前结束了循环,提高了效率,但是
}
}
break;
case 3:
System.exit(0);
break;
default:
break;
}
System.out.println("continue? <y/n>");
answer = input.next();
} while (answer.equals("y"));
}
}
class Student1{
private String name;
private int age;
private int stuNum;
public Student1() {
super();
// TODO Auto-generated constructor stub
}
public Student1(String name, int age, int stuNum) {
super();
this.name = name;
this.age = age;
this.stuNum = stuNum;
}
@Override
public String toString() {
return "Studetn [name=" + name + ", age=" + age + ", stuNum=" + stuNum
+ "]";
}
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 int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
}