-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentDemo.java
More file actions
132 lines (116 loc) · 4.54 KB
/
StudentDemo.java
File metadata and controls
132 lines (116 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.ithmm_04;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) {
//首先创建一个集合
ArrayList<Student> array = new ArrayList<Student>();
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);
//键盘接受用户选择
String line = sc.nextLine();
switch (line) {
case "1":
//添加学生信息
addStudent(array);
break;
case "2":
//删除学生
deleteStudent(array);
break;
case "3":
updateStudent(array);
//修改
break;
case "4":
findStudent(array);
//查看
break;
case "5":
//退出
System.out.println("谢谢使用");
System.exit(0);
default:
System.out.println("您输入的有误请重新输入");
}
}
}
public static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入姓名");
String name = sc.nextLine();
System.out.println("请输入年龄");
String age = sc.nextLine();
System.out.println("请输入学号");
String id = sc.nextLine();
System.out.println("请输入地址");
String address = sc.nextLine();
//new一个学生对象存放数据
Student s = new Student(name, age, id, address);
array.add(s);//将学生对象添加到集合中,this.什么=什么,将局部变量赋值给成员变量存放到一个集合数组中
}
public static void findStudent(ArrayList<Student> array) {
System.out.println("姓名\t\t学号\t\t年龄\t\t地址");
if(array.size() == 0){
System.out.println("暂无信息");
}else{
for (int i = 0;i<array.size();i++){
Student s = array.get(i);
System.out.println(s.getName()+"\t\t"+s.getId()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
}
//修改学生
public static void updateStudent(ArrayList<Student> array){
System.out.println("请输入你要修改的学生学号: ");
Scanner s = new Scanner(System.in);
String updateId= s.nextLine();
int index=getIndex(array,updateId);
if(index==-1){
System.out.println("查无此人");
}else{
System.out.println("请输入新的学生姓名");
String name = s.nextLine();
System.out.println("请输入新的学生年龄");
String age = s.nextLine();
System.out.println("请输入新的学生地址");
String address = s.nextLine();
Student s1 = new Student(name,updateId,age,address);
array.set(index,s1);
System.out.println("修改成功");
}
}
//删除学生信息
public static void deleteStudent(ArrayList<Student> array){
System.out.println("请输入你要删除的学生信息: ");
Scanner s = new Scanner(System.in);
String deleteId = s.nextLine();
int index = getIndex(array,deleteId);
if(index==-1){
System.out.println("查无此人");
}else{
array.remove(index);
System.out.println("删除成功");
}
}
//效验
public static int getIndex(ArrayList<Student> array,String Sid){
int index = -1;
for (int i = 0;i< array.size();i++){
Student s = array.get(i);
String id = s.getId();
//sid键盘输入的id
if(id.equals(Sid));{
index=i;
}
}
return index;
}
}