package Student;
/**
* @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+"\t"+"学生年龄:"+age;
}
}
package Student;
import java.util.Scanner;
/**
* @author 学生测试类
*/
public class StudentTest {
public static void main(String[] args) {
Student[] students = new Student[2];
for (int i = 0; i < students.length; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
Scanner sc1 = new Scanner(System.in);
System.out.println("请输入学生年龄:");
int age = sc1.nextInt();
Student s = new Student();
s.setName(name);
s.setAge(age);
students[i] = s;
}
for (int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
}
}
输出结果为:
请输入学生姓名:
张三
请输入学生年龄:
3
请输入学生姓名:
李四
请输入学生年龄:
4
学生姓名:张三 学生年龄:3
学生姓名:lisilislil李四 学生年龄:4
这个lisilislil是从哪来的 |