package Collection;
/**
* @author 标准学生类
*/
public class Student {
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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 String toString()
{
return "学生姓名:"+name+" 学生年龄:"+age;
}
}
package Collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
/**
*
* @author Administrator
*
* 1.首先定义一个标准学生类(name,age属性) 2.请用集合存储3个学生,然后遍历集合。打印出每个学生类的name和age。
*/
public class CollectionDemo1 {
public static void main(String[] args) {
Collection c = new ArrayList();
int i = 0;
while (i < 3) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
int age = sc.nextInt();
Student s = new Student();
s.setName(name);
s.setAge(age);
c.add(s);
i++;
}
Iterator it = c.iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
}
这个代码输不出结果
|