A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© emmm... 中级黑马   /  2017-11-25 21:57  /  1234 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

ArrayList<Student> array = new ArrayList<Student>();
  while (true){ System.out.println("0");System.out.println("1");
System.out.println("3");System.out.println("2");System.out.println("4")
System.out.println("5退出");Scanner sc = new Scanner(System.in);
String aString = sc.nextLine();switch (aString) {
case "1":findAllStudent(array); break;case "2":addStudent(array);
break;case "3":deleteStudent(array);break;case "4":
case "5":break;default:System.out.println("");
System.exit(0); break;}}}public static void updateStudent(ArrayList<Student>array){Scanner sc=new Scanner(System.in);
System.out.println("需要修改的学号");String id =sc.nextLine();int index=-1;
for (int i = 0; i < array.size(); i++) {Student s  =array.get(i);if(s.getId().equals(id)){
index=i;break;}} if(index==-1){System.out.println("");
  }else{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();
array.set(index, s);System.out.println(""); }}
public static void deleteStudent(ArrayList<Student>array){
Scanner sc=new Scanner(System.in);System.out.println(":");
String id =sc.nextLine(); int index=-1;
  for (int i = 0; i < array.size(); i++) {Student s =array.get(i);
if(s.getId().equals(id)){ index=i; break;} }if(index==-1){
   System.out.println("");}else{
    array.remove(index);
    System.out.println(""); }}
public static void addStudent(ArrayList<Student> array) {
  Scanner sc = new Scanner(System.in);
  String id;while (true) {System.out.println(":"); id = sc.nextLine();
   boolean flag = falsefor (int x = 0; x < array.size(); x++) {Student s = array.get(x);
    if (s.getId().equals(id)) { flag = true;break; } }
   if (flag) {
    System.out.println("");
   } else {
  break
   }
  }
  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.setId(id);
  s.setName(name);
  s.setAge(age);
  s.setAddress(address);
  array.add(s);
  System.out.println("添加学生成功");
}
public static void findAllStudent(ArrayList<Student> array) {
  if (array.size() == 0) {
   System.out.println("还没录入信息");
   return;
  }
  System.out.println("学号\t姓名\t年龄\t居住地");
  for (int i = 0; i < array.size(); i++) {
   Student s = array.get(i);
   System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
  }
}
}

6 个回复

倒序浏览
回复 使用道具 举报
看不懂
回复 使用道具 举报
楼上的,,看不懂的不是你一个人
回复 使用道具 举报
厉害厉害
回复 使用道具 举报
回复 使用道具 举报
哇哇哇,总结真到位
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马