import java.util.ArrayList;
import java.util.Scanner;import com.sun.org.apache.xpath.internal.Arg;
public class Student {
//两个私有属性
private int no ;
private String name ;
//默认构造函数
public Student(){}
//带参构造函数
public Student(int no,String name){
this.no=no;
this.name=name;
}
//添加学生信息
public void addStudentInfo(ArrayList list){
String flag1="yes";
do{
Student student=new Student();
Scanner in =new Scanner(System.in);
System.out.println("请输入学生学号:");
student.setNo(in.nextInt());
System.out.println("请输入学生姓名:");
student.setName(in.next());
list.add(student);
System.out.println("是否继续添加学生信息(yes/no)?");
flag1=in.next();
} while(flag1.equals("yes"));
}
//读取学生信息
public void showStudentInfo(ArrayList
System.out.println(" 学生no: "+" 学生name: ");
for(Student student:list){
System.out.println(" "+student.getNo()+" "+student.getName());
}
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String arg[]) {
ArrayList
Student student= new Student();
Scanner inn= new Scanner(System.in);
while(true){
System.out.println("----------------- 欢迎使用学生管理系统------------------");
System.out.println("1.输入学生信息");
System.out.println("2.显示学生信息");
System.out.println("0.退出系统");
System.out.println("请选择....");
int nn= inn.nextInt();
switch (nn) {
case 1:
student.addStudentInfo(studentList);
break;
case 2:
System.out.println("学生信息如下:");
student.showStudentInfo(studentList);
break;
case 0:
System.exit(0);
System.out.println("系统退出!");
break;
default:
break;
}
}
} }
Java 代码如下:import java.util.ArrayList;
import java.util.Scanner;public class student{
private int number;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public student(){}
public student(int number, String name) {
this.number = number;
this.name = name;
}
public student writeInfo(){
student st = new student();
Scanner in = new Scanner(System.in);
System.out.println("请输入学生的学号:");
number = in.nextInt();
st.setNumber(number);
System.out.println("请输入学生的姓名:");
String name = in.next();
st.setName(name);
return st;
}
public void readInfo(ArrayList list){
for(int i=0;i<=list.size()-1;i++){
student st = new student();
st = (student) list.get(i);
System.out.println("第"+(i+1)+"位学生的学号:" + st.getNumber());
System.out.println("第"+(i+1)+"位学生的姓名:" + st.getName());
}
}
public static void main(String[]args){ ArrayList
student st = new student();
Scanner in = new Scanner(System.in);
while(true){
System.out.println("1、输入学生信息");
System.out.println("2、输出学生信息");
System.out.println("请选择:");
int option = in.nextInt();
switch (option) {
case 1:
list.add(st.writeInfo());
break;
case 2:
st.readInfo(list);
break;
default:
break;
}
}
}
}