題目:錄入學員信息並保存,當錄入學員的編號為0時結束,展示出學員信息
//student類
public class Student {
public int id;
public String name;
public int age;
public Student() {
super();
}
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
//text
public class Text {
public static void main(String[] args) {
System.out.println("*****錄入學員信息,當錄入學員的編號為0時結束*****");
Scanner input = new Scanner(System.in);
ArrayList<Student> arraylist = new ArrayList<Student>();
//輸入學員信息
while(true){
Student student = new Student();
System.out.print("請輸入學員學號:");
student.id = input.nextInt();
if(student.id==0){
System.out.println("結束錄入!");
break;
}else{
System.out.print("請輸入學員姓名:");
student.name = input.next();
System.out.print("請輸入學員年齡:");
student.age = input.nextInt();
arraylist.add(student);
}
}
//輸出學員信息
System.out.println("學員的信息如下:");
System.out.println("學號\t姓名\t年齡");
Iterator<Student> iterator = arraylist.iterator();
while(iterator.hasNext()){
Student a = iterator.next();
System.out.println(a.id+"\t"+a.name+"\t"+a.age);
}
}
運行結果: