要求编写一个java程序,输入学生信息,并能保存与显示学生信息。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了要求编写一个java程序,输入学生信息,并能保存与显示学生信息。相关的知识,希望对你有一定的参考价值。
实现:1.实现一个学生类。包括学号(int)和姓名(string)。 2.使用ArrayList保存学生信息。 3.输出界面: 1.输入学生信息 2.显示学生信息 请选择:
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<Student> list)
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> studentList= new ArrayList<Student>();
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;
参考技术A 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> list = new ArrayList<student>();
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;
java定义一个学生类要求学号,姓名,年龄,三个成员变量
两个构造方法参数分别为:学号、姓名和;学号、姓名、年龄。要使用THIS关键字,将接收到的实参的值为每一个对应的成员变量赋值。请大家帮帮忙在线等
class Studentprivate int STU_NUM;
private String STU_NAME;
private int STU_AGE;
public Student(int NUM,String NAME)
this.STU_NAME=NAME;
this.STU_NUM=NUM;
public Student(int NUM,String NAME,int AGE)
this.STU_NAME=NAME;
this.STU_NUM=NUM;
this.STU_AGE=AGE;
参考技术A public class Student
public int Sno; //学号
public String Sname; //Student Name;
public int Sage; //年龄
public Student()
//无参构造器
public Student(int Sno,String Sname, int Sage)
this.Sno=Sno;
this.Sname=Sname;
this.Sage=Sage;
如果要封装, 将属性改为 private 的话 继续追问 参考技术B public class Student
private String number;
private int age;
private String name;
Student(String number,String name)
this.number = number;
this.name = name;
Student(String number,String name,int age)
this.number = number;
this.name = name;
this.age = age;
public String getNumber()
return number;
public void setNumber(String number)
this.number = number;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
以上是关于要求编写一个java程序,输入学生信息,并能保存与显示学生信息。的主要内容,如果未能解决你的问题,请参考以下文章
设计一个程序,要求完成以下功能,在代码编写过程中,请注意Java编程规范。 1、程序要求把所有学生的信息