Java基础之集合

Java基础之集合

七月 07, 2019 阅读数(请刷新)

利用list集合编写的终极武器系统

用户操作

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
package Demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class crud {

static List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();

public List<Map<String, Object>> add( String name, String password) {
Map<String, Object> map = new HashMap<String, Object>();
int id ;
if(list.size()==0){
id=1;
}
else{
id= new Random().nextInt(1000);
}
map.put("id", id);
map.put("name", name);
map.put("password", password);
list.add(map);
return list;
}

public int delete(String id) {
for (int i = 0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
if(id.equals(map.get("id").toString())){
list.remove(i);
return 0;
}
}
return 1;
}

public List<Map<String, Object>> show() {
return list;
}

public void update(String updateid, String str) {
Map<String, Object> newmap = new HashMap<String, Object>();
newmap.put("id", updateid);
//用split('-')方法将字符串以"-"分割形成一个字符串数组,然后再通过索引[0]取出所得数组中的第1个元素的值
newmap.put("name", str.split("-")[0]);
newmap.put("password", str.split("-")[1]);

for (int i = 0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
if(updateid.equals(map.get("id").toString())){
list.set(i, newmap);
}
}

}

public void em() {
for (int i = 0; i < list.size(); i++) {
list.remove(i);

}

}




}

登录操作

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package Demo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Login {
static int sum =0;
public static Scanner sc = new Scanner(System.in);
static int i =0;
static crud crud = new crud();
static weapon weapon = new weapon();
public static void main(String[] args) {
int sum =0;
System.out.println("----------欢迎使用终极武器系统------------");
System.out.println(" ------1.登录---------2.注册------");

int index = sc.nextInt();
if(index==1){
//登录
while (true) {
System.out.println("请输入您的用户名");
String name = sc.next();
System.out.println("请输入您的密码");
String pwd = sc.next();
if(name.equals("admin")&&pwd.equals("admin")){
System.out.println("您好,将军!您已获取最高权限,系统有自爆程序,请小心使用!");
loginAdmin();
return;
}
List<Map<String, Object>> list = crud.show();
if(list.size()==0){
System.out.println("暂无用户");
main(null);
return;
}
for (int i = 0; i < list.size(); i++) {
if(name.equals(list.get(i).get("name").toString())){
if(pwd.equals(list.get(i).get("password").toString())){
System.out.println("登录成功");
loginOk();
}
else{
System.out.println("密码错误");
sum++;
}
}
else{
System.out.println("用户名错误");
sum++;
}
}
if(sum==3){
System.out.println("您已累计三次错误。。。");
main(null);
return;
}
}

}
if(index==2){
//注册 注册成功的信息存入到数组中
// 二维数组
user_name();
}
}


private static void loginOk() {
System.out.println("--1.查看武器装备------2.选择武器-----3.发射-----4.回收武器-----0.退出");
// 500 枪支 10000
// 40 高射炮 500
// 5000 投掷武器
String weaponindex = sc.next();
if(weaponindex.equals("1")){
List<Map<String, Object>> list = weapon.showRm();
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
loginOk();
}

if(weaponindex.equals("2")){
boolean t=true;
while(t) {
System.out.println("选择武器类型:");
List<Map<String, Object>> list = weapon.showRm();
for (int i = 0; i < list.size(); i++) {
System.out.println(i+1 + ":");
System.out.println(list.get(i));
}


String type = sc.next();
if(type.equals("1")) {
System.out.println("请选择枪支数量:");
int amount = sc.nextInt();
if(amount>(int)list.get(0).get("数量")) {
boolean a=true;
while(a) {
System.out.println("没有那么多枪支可供选择,请重新输入!");
amount = sc.nextInt();
if(amount<=(int)list.get(0).get("数量")) {
a=false;
}
}
}

System.out.println("请选择弹药数量:");
int ammunition = sc.nextInt();
if(ammunition>(int)list.get(0).get("弹药数")) {
boolean a=true;
while(a) {
System.out.println("没有那么多弹药可供选择,请重新输入!");
ammunition = sc.nextInt();
if(ammunition<=(int)list.get(0).get("弹药数")) {
a=false;
}
}
}
weapon.add(type,amount,ammunition);
}
else if(type.equals("2")) {
System.out.println("请选择高射炮数量:");
int amount = sc.nextInt();
if(amount>(int)list.get(1).get("数量")) {
boolean a=true;
while(a) {
System.out.println("没有那么多高射炮可供选择,请重新输入!");
amount = sc.nextInt();
if(amount<=(int)list.get(1).get("数量")) {
a=false;
}
}
}
System.out.println("请选择导弹数量:");
int ammunition = sc.nextInt();
if(ammunition>(int)list.get(1).get("导弹数")) {
boolean a=true;
while(a) {
System.out.println("没有那么多导弹可供选择,请重新输入!");
ammunition = sc.nextInt();
if(ammunition<=(int)list.get(1).get("导弹数")) {
a=false;
}
}
}
weapon.add(type,amount,ammunition);
}
else if(type.equals("3")) {
System.out.println("请选择投掷武器数量:");
int amount = sc.nextInt();
if(amount>(int)list.get(2).get("数量")) {
boolean a=true;
while(a) {
System.out.println("没有那么多投掷武器可供选择,请重新输入!");
amount = sc.nextInt();
if(amount<=(int)list.get(2).get("数量")) {
a=false;
}
}
}
int ammunition = 0;
weapon.add(type,amount,ammunition);
}
System.out.println("是否继续选择?[y/n]");
String yn = sc.next();
if(yn.equals("y")||yn.equals("Y")) {
t=true;
}
else if(yn.equals("n")||yn.equals("N")) {
t=false;
}
}
List<Map<String, Object>> listUser = weapon.showRm1();
for (int i = 0; i < listUser.size(); i++) {
System.out.println("选择的武器:");
System.out.println(listUser.get(i));
}
loginOk();
}
if(weaponindex.equals("3")){

System.out.println("请选择使用武器:");
List<Map<String, Object>> listUsers = weapon.showRm1();
if(listUsers.size()==0) {
System.out.println("没有武器,请选择武器作战!");
loginOk();
}
for (int i = 0; i < listUsers.size(); i++) {
System.out.println(i+1 + ":");
System.out.println(listUsers.get(i));
}
int type1 = sc.nextInt();

weapon.div(type1);

loginOk();
}
if(weaponindex.equals("4")){
List<Map<String, Object>> listUsers = weapon.showRm1();
if(listUsers.size()==0) {
System.out.println("没有武器可回收!");
loginOk();
}
weapon.recycle();
System.out.println("回收成功");
loginOk();
}
if(weaponindex.equals("0")){

main(null);
}
}


private static void loginAdmin() {
System.out.println("--1.查看------2.添加-----3.移除------4.更改----5自爆----返回上一层/P ");
String logindex = sc.next();
if(logindex.equals("1")){
List<Map<String, Object>> list = crud.show();
if(list.size()==0){
System.out.println("暂无数据,请去添加");
loginAdmin();
}
else{
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
loginAdmin();
}

}
if(logindex.equals("2")){
System.out.println("请输入姓名");
String name = sc.next();
System.out.println("请输入密码");
String password = sc.next();
crud.add(name, password);
System.out.println("添加成功");
loginAdmin();
}

if(logindex.equals("3")){
List<Map<String, Object>> list = crud.show();
for (int i = 0; i < list.size(); i++) {
int a=i+1;
System.out.println("编号:"+a+list.get(i));
}
if(list.size()==0) {
System.out.println("暂无用户!");
loginAdmin();
}
System.out.println("请输入移动信息的编号");
String id= sc.next();
int deletesum =crud.delete(id);

if(deletesum==0){
System.out.println("删除成功");
loginAdmin();
}
else{
System.out.println("删除失败");
loginAdmin();
}


}

if(logindex.equals("4")){
int sum=0;

List<Map<String, Object>> list = crud.show();
for (int i = 0; i < list.size(); i++) {
int b=i+1;
System.out.println("编号:"+b+list.get(i));
}
if(list.size()==0) {
System.out.println("暂无用户!");
loginAdmin();
}
System.out.println("请选择您要修改的数据的编号");
String updateid = sc.next();
for (int i = 0; i < list.size(); i++) {
if(updateid.equals(list.get(i).get("id").toString())){

System.out.println(list.get(i));
sum++;
}
}
if(sum==1){
System.out.println("请输入您要修改的数据内容 (姓名-密码)");
String str = sc.next();
crud.update(updateid,str);
System.out.println("修改成功");
loginAdmin();
}else{
System.out.println("您输入的编号有误,暂无这条数据");
loginAdmin();
}


}
if(logindex.equals("5")) {
System.out.println("自爆程序已启动,系统将于5秒后自爆,请尽快撤离(你应该跑不了了)!!!");
List<Map<String, Object>> list = crud.show();
crud.em();
int a=5;
while(true) {
System.out.println(a--);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(a==0) {
System.out.println("boom,嘣。。。boom。。。!!!");
main(null);
}
}
}
if(logindex.equals("p")){
main(null);
}

}


private static String user_password( String uname ) {
System.out.println("请输入密码");
String upwd = sc.next();
if(upwd.length()>=4 && upwd.length()<=8 ){

crud.add(uname, upwd);

System.out.println("注册成功");
main(null);
}
else{
System.out.println("请输入正确的4-8位长度密码");
user_password(uname);
}
return null;
}
private static String user_name() {

System.out.println("请输入用户名");
String uname = sc.next();
if(uname.length()>=4 && uname.length()<=8 ){
user_password(uname);
}
else{
System.out.println("请输入正确的4-8位用户名");
user_name();
}
return null;
}
}

武器操作

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package Demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class weapon {

static int gun = 500;
static int archibald = 40;
static int grenade = 5000;
static int ammunition1 = 10000;
static int ammunition2 = 500;
public static Scanner sc = new Scanner(System.in);
static List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
static List<Map<String, Object>> listUser = new ArrayList<Map<String,Object>>();
static{

Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("数量", gun);
map1.put("种类", "枪支");
map1.put("弹药数", ammunition1);

Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("数量", archibald);
map2.put("种类", "高射炮");
map2.put("导弹数", ammunition2);

Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("数量", grenade);
map3.put("种类", "投掷武器");


list.add(map1);
list.add(map2);
list.add(map3);
}


public List<Map<String, Object>> showRm() {
return list;
}
public List<Map<String, Object>> showRm1() {

return listUser;
}

public void add(String type, int amount, int ammunition) {

if(type.equals("1")){
int a =(int)list.get(0).get("数量")-amount;
int b =(int)list.get(0).get("弹药数")-ammunition;
Map<String, Object> map = new HashMap<String, Object>();
map.put("数量", a);
map.put("种类", "枪支");
map.put("弹药数", b);
list.set(0, map);

}
if(type.equals("2")){
int a =(int)list.get(1).get("数量")-amount;
int b =(int)list.get(1).get("导弹数")-ammunition;
Map<String, Object> map = new HashMap<String, Object>();
map.put("数量", a);
map.put("种类", "高射炮");
map.put("导弹数", b);
list.set(1, map);
}

if(type.equals("3")){
int a =(int)list.get(2).get("数量")-amount;
Map<String, Object> map = new HashMap<String, Object>();
map.put("数量", a);
map.put("种类", "投掷武器");
list.set(2, map);
}

Map<String, Object> map = new HashMap<String, Object>();
if(type.equals("1")) {
map.put("选择武器种类", "枪支");
map.put("数量", amount);
map.put("弹药数", ammunition);}
else if(type.equals("2")) {
map.put("选择武器种类", "高射炮");
map.put("数量", amount);
map.put("导弹数", ammunition);}
else if(type.equals("3")) {
map.put("选择武器种类", "投掷武器");
map.put("数量", amount);
}

listUser.add(map);
}
public void div(int type1) {

if("枪支".equals(listUser.get(type1-1).get("选择武器种类").toString())) {
System.out.println("请选择使用枪支数量:");
int n = sc.nextInt();
if(n>(int)listUser.get(type1-1).get("数量")) {
boolean a=true;
while(a) {
System.out.println("枪支不够,请重新输入!");
n = sc.nextInt();
if(n<=(int)listUser.get(type1-1).get("数量")) {
a=false;
}
}
}
System.out.println("请选择使用弹药数量:");
int d = sc.nextInt();
if(d>(int)listUser.get(type1-1).get("弹药数")) {
boolean a=true;
while(a) {
System.out.println("弹药不够,请重新输入!");
d = sc.nextInt();
if(d<=(int)listUser.get(type1-1).get("弹药数")) {
a=false;
}
}
}
Map<String, Object> newmap = new HashMap<String, Object>();
newmap.put("选择武器种类", "枪支");
newmap.put("数量",(int)listUser.get(type1-1).get("数量")-n);
newmap.put("弹药数",(int)listUser.get(type1-1).get("弹药数")-d);
listUser.set(type1-1, newmap);
System.out.println("发射:");
boolean t=true;
int j=0;
while(t) {
System.out.print("哒");
n--;
int a=(int) (Math.random()*10);
if(a==6) {
j++;
}

try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(n==0) {
System.out.println(" 发射完毕,共击中"+j+"个敌军!!!");
t=false;
}
}

}
else if("高射炮".equals(listUser.get(type1-1).get("选择武器种类").toString())) {
System.out.println("请选择使用高射炮数量:");
int n = sc.nextInt();
if(n>(int)listUser.get(type1-1).get("数量")) {
boolean a=true;
while(a) {
System.out.println("高射炮不够,请重新输入!");
n = sc.nextInt();
if(n<=(int)listUser.get(type1-1).get("数量")) {
a=false;
}
}
}
System.out.println("请选择使用导弹数量:");
int d = sc.nextInt();
if(d>(int)listUser.get(type1-1).get("导弹数")) {
boolean a=true;
while(a) {
System.out.println("导弹不够,请重新输入!");
d = sc.nextInt();
if(d<=(int)listUser.get(type1-1).get("导弹数")) {
a=false;
}
}
}
Map<String, Object> newmap = new HashMap<String, Object>();
newmap.put("选择武器种类", "高射炮");
newmap.put("数量",(int)listUser.get(type1-1).get("数量")-n);
newmap.put("导弹数",(int)listUser.get(type1-1).get("导弹数")-d);
listUser.set(type1-1, newmap);
System.out.println("发射:");
boolean t=true;
int j = 0;
while(t) {
System.out.print("轰");
n--;
int a=(int) (Math.random()*10);
if(a==6) {
j++;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(n==0) {
System.out.println("发射完毕,共击中"+j+"个敌军!!!");
t=false;
}
}

}
else if("投掷武器".equals(listUser.get(type1-1).get("选择武器种类").toString())) {
System.out.println("请选择使用投掷武器数量:");
int n = sc.nextInt();
if(n>(int)listUser.get(type1-1).get("数量")) {
boolean a=true;
while(a) {
System.out.println("投掷武器不够,请重新输入!");
n = sc.nextInt();
if(n<=(int)listUser.get(type1-1).get("数量")) {
a=false;
}
}
}
Map<String, Object> newmap = new HashMap<String, Object>();
newmap.put("选择武器种类", "投掷武器");
newmap.put("数量",(int)listUser.get(type1-1).get("数量")-n);

listUser.set(type1-1, newmap);
System.out.println("投掷:");
boolean t=true;
int j = 0;
while(t) {
System.out.print("boom ");
n--;
int a=(int) (Math.random()*10);
if(a==6) {
j++;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(n==0) {
System.out.println("投掷完毕,共击中"+j+"个敌军!!!");
t=false;
}
}

}
}
public void recycle() {

for (int i = 0; i < listUser.size(); i++) {

if("枪支".equals(listUser.get(i).get("选择武器种类").toString()))
{
Map<String, Object> newmap = new HashMap<String, Object>();
newmap.put("种类", "枪支");
newmap.put("数量",(int)listUser.get(i).get("数量")+(int)list.get(0).get("数量"));
newmap.put("弹药数",(int)listUser.get(i).get("弹药数")+(int)list.get(0).get("弹药数"));

list.set(0, newmap);
}
else if("高射炮".equals(listUser.get(i).get("选择武器种类").toString()))
{
Map<String, Object> newmap = new HashMap<String, Object>();
newmap.put("种类", "高射炮");
newmap.put("数量",(int)listUser.get(i).get("数量")+(int)list.get(1).get("数量"));
newmap.put("导弹数",(int)listUser.get(i).get("导弹数")+(int)list.get(1).get("导弹数"));

list.set(1, newmap);
}
else if("投掷武器".equals(listUser.get(i).get("选择武器种类").toString()))
{

Map<String, Object> newmap = new HashMap<String, Object>();
newmap.put("种类", "投掷武器");
newmap.put("数量",(int)listUser.get(i).get("数量")+(int)list.get(2).get("数量"));

list.set(2, newmap);

}
}
listUser.clear();
}



}

集合总结

1、list集合

创建:List<泛型> list= new ArrayList<泛型>();
添加方法:add()
list list= new ArrayList();
list.add(“你好”);
更新方法:set(index, element)
String str = “你好,Java!”
list.set(0,str)
移除方法:remove(index);
list.add(“2”);
list.remove(0);
获取方法:get(index);
list.get(0);
进阶:List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
添加:Map<String, Object> map1 = new HashMap<String, Object>();
map1.put(“数量”, gun);
map1.put(“种类”, “枪支”);
map1.put(“弹药数”, ammunition1);
list.add(map1);
list.get(0).get(“数量”);//list集合数组中的第一个的数量对应的值gun;

2、map集合

创建:Map<k,v> map = new HashMap<k,v>();
k表示键值,v为对应的值,可根据k值找到对相应的v值
如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
List<Map<String, Object>>  list  = new ArrayList<Map<String,Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("数量", gun);
map1.put("种类", "枪支");
map1.put("弹药数", ammunition1);

Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("数量", archibald);
map2.put("种类", "高射炮");
map2.put("导弹数", ammunition2);

Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("数量", grenade);
map3.put("种类", "投掷武器");


list.add(map1);
list.add(map2);
list.add(map3);
list.get(0).get("数量");//list集合数组中的第一个的数量对应的值gun;

添加方法:put();
Map<String,Object> map = new HashMap<String,Object>();
map.put();