标签:you public 标准 arraylist array get while lis hang
import java.util.Scanner;
import java.util.ArrayList;
import java.util.;
//主界面
/
1、用输出语句完成主界面的编写
2、用Scannner实现键盘录入
3、用switch完成操作选择
4、循环回到主界面
*/
public class jiaodashu
{
public static void main(String[] args)
{
//创价集合,用以存储学生
ArrayList
while(true)
{
System.out.println("--------学生管理系统--------");
System.out.println("1、添加学生");
System.out.println("2、删除学生");
System.out.println("3、修改学生");
System.out.println("4、查看所有学生");
System.out.println("5、退出");
System.out.println("输入你的选择:");
Scanner sc = new Scanner(System.in);
int yourOption = sc.nextInt();
switch(yourOption)
{
case 1:
addStudent(array);
break;
case 2:
deledeStudent(array);
break;
case 3:
changeStudent(array);
break;
case 4:
lookAllStudent(array);
break;
case 5:
System.out.println("退出");
return;
}
}
}
//添加学生
public static void addStudent(ArrayList<Student> array)
{
Scanner sc = new Scanner(System.in);
System.out.println("输入学号");
String sid = sc.nextLine();
System.out.println("输入姓名");
String name = sc.nextLine();
System.out.println("输入年龄");
String age = sc.nextLine();
System.out.println("输入居住地");
String address = sc.nextLine();
//
Student s= new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
//删除学生
public static void deledeStudent(ArrayList<Student> array)
{
Scanner sc = new Scanner(System.in);
System.out.println("输入你要删除学生的学号");
String sid = sc.nextLine();
for(int i=0;i<array.size();i++)
{
Student s = array.get(i);
if(s.getSid().equals(sid))
{
array.remove(i);
break;
}
}
System.out.println("删除成功");
}
//修改学生
public static void changeStudent(ArrayList<Student> array)
{
Scanner sc = new Scanner(System.in);
System.out.print("输入你要修改的学生学号:");
String sid = sc.nextLine();
System.out.print("输入新姓名:");
String name = sc.nextLine();
System.out.print("输入新年龄:");
String age = sc.nextLine();
System.out.print("输入新地址:");
String address = sc.nextLine();
Student nc = new Student();
nc.setSid(sid);
nc.setName(name);
nc.setAge(age);
nc.setAddress(address);
for(int i=0;i<array.size();i++)
{
Student s = array.get(i);
if(s.getSid().equals(sid))
{
array.set(i,nc); //在i位置插入一个类
break;
}
}
System.out.println("修改成功");
}
//查看学生
public static void lookAllStudent(ArrayList<Student> array)
{
System.out.println("学号\t姓名\t年龄\t居住地");
for(int i=0;i<array.size();i++)
{
Student s = array.get(i);
System.out.println(s.getSid()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
}
public class Student //标准类
{
private String sid;
private String name;
private String age;
private String address;
public Student()
{
//无参构造方法
}
public Student(String sid,String name,String age, String address)
{
//this让外部可以访问这些东西
this.sid=sid;
this.name = name;
this.age = age;
this.address = address;
}
public void setSid(String sid)
{
this.sid=sid;
}
public void setName(String name)
{
this.name = name;
}
public void setAge(String age)
{
this.age = age;
}
public void setAddress(String address)
{
this.address = address;
}
public String getSid()
{
return sid;
}
public String getName()
{
return name;
}
public String getAge()
{
return age;
}
public String getAddress()
{
return address;
}
}
标签:you public 标准 arraylist array get while lis hang
原文地址:https://www.cnblogs.com/tianyb/p/12755264.html