黑马程序员技术交流社区

标题: 07 [打印本页]

作者: emmm...    时间: 2017-11-25 21:57
标题: 07
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());
  }
}
}


作者: 陈文老师    时间: 2017-11-25 23:52
可以的,可以的

作者: 1414225997    时间: 2017-11-27 17:38
看不懂
作者: xiongliu    时间: 2017-11-28 10:55
楼上的,,看不懂的不是你一个人
作者: 渝鱼鱼    时间: 2017-11-29 11:10
厉害厉害
作者: 渝小妹    时间: 2017-11-29 16:37
加油  加油

作者: orson...    时间: 2017-11-30 09:19
哇哇哇,总结真到位




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2