上午

今天上午先写写昨天晚上的做题的情况吧。。热身赛是晚上八点左右开始的,然后自己就差不多一直在做题了,一直做到了十一点左右,才回宿舍。比赛平台需要连接VPN才能访问,,由于是热身赛,题目并不是非常难,不好说哪个最简单,脑洞题目有很多,说实话没有大佬们的提示,自己真的很难做出来,有个求最短路径的,算是比较简单的吧,回到宿舍又看了看,感觉正式比赛可能不会非常的简单。希望自己能再学习一波吧~

下面再说说今天上午做的东西,今天上午主要是做文件操作,将用户注册信息写进文件进行保存,这样程序运行结束后,用户信息也能够保存下来,以后继续使用。来时简单的说了一下文件操作的一些基本的步骤,剩下的自己扩展就好了。由于每次写文件的时候都会把原文件的内容覆盖写入新的程序,一时没想到好的办法,就先做了一次读文件的操作,用一个字符串保存原文件的内容,然后把要写入的内容连接到字符串再上一起写入文件,从而达到追加的效果。上午主要是熟悉了一下文件操作,以及写了一个文件操作的类,把初始化创建文件、读文件和写文件的功能写了一下,基本达到了建用户信息按照用户名+密码+积分的形式按行记录到文件中的目的。
这里是完善后的代码,除了文件操作,又加入了排行榜的功能,能够显示当前积分最高的前三位(相同积分的按照注册先后顺序先注册的排名高):
login.java

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

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;


public class login {

private static String Name[] = new String [1000];
private static String Pass[] = new String [1000];
private static String Point[] = new String [1000];
private static int usernum = 0;

private static Scanner input = new Scanner(System.in);

public static void main(String [] args) throws IOException{


iotest1.createfile();
init();
while(true){
System.out.println("**************************************");
System.out.println("** 欢迎来到剪刀石头布游戏 **");
System.out.println("** **");
System.out.println("** 1.登录 2.注册 3.天梯 4.退出 **");
System.out.println("**************************************");
int select = input.nextInt();
if(select == 1){
//登录
if(usernum == 0){
System.out.println("系统暂无注册用户,请先注册!");
}else {
System.out.println("请输入用户名:");
String temname = input.next();
int i = 0,flag = 0;
while(flag==0){
for(i = 0;i < usernum;i++){
if(temname.equals(Name[i])){
flag = 1;
break;
}
}
if(flag == 0){
System.out.println("该用户不存在!请重新输入用户名:");
temname = input.next();
}
}
System.out.println("请输入密码:");
String tempass = input.next();
int time = 0;
while(tempass.equals(Pass[i])==false){
time++;
if(time==5){
break;
}
System.out.println("密码输入错误!请重新输入密码:");
tempass = input.next();
}
if(time==5){
System.out.println("密码输入次数过多!登陆失败!");
break;
}else{
System.out.println("登录成功!\n 欢迎用户" + temname + "登录剪刀石头布游戏!");
int sel = 1;
while(sel == 1){
int pointflag = guess.main(null);//开始玩游戏
if(pointflag == 1){//记录每次游戏后的积分情况
int pt = Integer.parseInt(Point[i]);
pt += 1;
Point[i] = String.valueOf(pt);
}else if(pointflag == 2){
int pt = Integer.parseInt(Point[i]);
pt += 5;
Point[i] = String.valueOf(pt);
}else {
int pt = Integer.parseInt(Point[i]);
if(pt > 0){
pt -= 1;
Point[i] = String.valueOf(pt);
}else{
Point[i]="0";
}
}
System.out.println("**************************************");
System.out.println("** 是否再来一局? **");
System.out.println("** **");
System.out.println("** 1.再来一局 2.退出登录 **");
System.out.println("**************************************");
sel = input.nextInt();
}

if(sel!=2){
System.out.println("恭喜你中彩蛋啦!!!!!异常输入~积分加100~游戏退出!");
Point[i] = String.valueOf(Integer.parseInt(Point[i])+100);
}
//这里应该询问用户是否再来一局(加个循环)

//System.out.println("后续功能待开发!再见!");
//break;
}
}
}else if(select == 2){
//注册
System.out.println("******欢迎注册剪刀石头布游戏********");
System.out.println("请输入用户名(用户名不得少于4位!):");
String name = input.next();
int i,flag;
while(true){
if(name.length()<4){
System.out.println("用户名过短!请重新输入用户名!");
name = input.next();
}else {
i = 0;
flag = 0;
while(flag==0){
for(i = 0;i < usernum;i++){
if(name.equals(Name[i])){
flag = 1;
break;
}
}
if(flag == 1){
System.out.println("该用户已存在!请重新输入用户名:");
name = input.next();
}else {
break;
}
}
if(flag == 0){
break;
}
}


}

System.out.println("请输入用户密码(密码不得少于4位!):");
String pass = input.next();
while(true){
if(pass.length()<4){
System.out.println("密码过短!请重新输入密码!");
pass = input.next();
}else {
break;
}
}
System.out.println("请再次输入确认密码:");
while(input.next().equals(pass)==false){
System.out.println("两次输入密码不一致,注册失败!\n 请再次确认密码!");
}
if(usernum<1000){//防止内存溢出
//iotest.temreadTxtFile();
//iotest.writeTxtFile(name + " " + pass);
String tss = iotest1.readfile();
iotest1.writefile(tss + name + " " + pass + " " + "0");//更新注册文件

Name[usernum]=name;//更新内存中的注册信息,速度更快
Pass[usernum]=pass;
Point[usernum]="0";
usernum++;
//regflag=1;
System.out.println("用户" + name + "注册成功!");
//break;
}else{
System.out.println("系统用户注册数已达最大值!注册失败!");
break;
}

/*String doublepass = input.next();

if(pass.equals(doublepass)){
System.out.println("注册成功!");
break;
}else {
System.out.println("两次输入密码不一致,注册失败!\n 请重新输入密码!");
while(input.next().equals(pass)==false){
System.out.println("两次输入密码不一致,注册失败!\n 请重新输入密码!");
}
System.out.println("注册成功!");
break;
}*/

}else if(select == 4){
//退出
//退出前更新用户的积分情况信息将内存中所有的用户信息重新整理写入注册文件
String refresh = "";
for(int i = 0; i < usernum ; i++){
refresh += Name[i] + " " + Pass[i] + " " + Point[i] + "\n";

}
iotest1.writefile(refresh);//更新

System.out.println("************************************");

/* //System.out.println(usernum);//测试代码
for(int i = 0 ;i < usernum ; i++){
System.out.println(Name[i]);
}
for(int i = 0 ;i < usernum ; i++){
System.out.println(Pass[i]);
}
for(int i = 0 ;i < usernum ; i++){
System.out.println(Point[i]);
}*/
System.out.println("**************************************");
//System.out.println("退出前显示注册信息:");
//String usermsg = iotest1.readfile();
//System.out.println(usermsg);

System.out.println("欢迎下次再来!谢谢!");
break;
}else if(select == 3){
//天梯
showranks();
}else {
//非法输入
System.out.println("该功能尚未开发,敬请期待!");
}
}
}
private static void showranks() {
// TODO Auto-generated method stub
int i = 0;
int temPoint [] = new int [usernum];

for(i = 0;i < usernum; i++){
temPoint[i] = Integer.parseInt(Point[i]);
}
Arrays.sort(temPoint);
String first="";
String second="";
String third="";
int firstPoint = 0,secondPoint = 0,thirdPoint = 0;

for(i = 0;i<usernum;i++){
if(Integer.parseInt(Point[i])==temPoint[usernum-1]){
firstPoint = temPoint[usernum-1];
temPoint[usernum-1]=0;
first=Name[i];
break;
}
}
Arrays.sort(temPoint);
for(i = 0;i<usernum;i++){
if(Integer.parseInt(Point[i])==temPoint[usernum-1]){
secondPoint = temPoint[usernum-1];
temPoint[usernum-1]=0;
if(!Name[i].equals(first)){
second=Name[i];
break;
}else {
continue;
}

}
}
Arrays.sort(temPoint);
for(i = 0;i<usernum;i++){
if(Integer.parseInt(Point[i])==temPoint[usernum-1]){
if(!(Name[i].equals(first)||Name[i].equals(second))){
thirdPoint = temPoint[usernum-1];
third=Name[i];
break;
}else {
continue;
}

}
}
System.out.println("**************************************");
System.out.println("**************************************");
System.out.println("** 天梯排行榜 **");
System.out.println("** First Place:" + first + " " + firstPoint + " **");
System.out.println("** Second Place:" + second + " " + secondPoint + " **");
System.out.println("** Third Place:" + third + " " + thirdPoint + " **");
System.out.println("**************************************");
System.out.println("**************************************");
}
public static void init() throws IOException{//将用户信息文件读入内存数组中
String temss = iotest1.readfile();
String [] ts = temss.split("\n");
for(int i = 0;i < ts.length;i++){
//System.out.println(ts[i]);
//System.out.println("");
String [] tts = ts[i].split(" ");
Name[usernum] = tts[0];
Pass[usernum] = tts[1];
Point[usernum] = tts[2];
usernum++;
}
}
}


iotest1.java

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

import java.io.*;
import java.util.Scanner;


public class iotest1 {
private static String path = "D:/usermessage.txt";
private static File filename = new File(path);


/**
* 创建文本文件.
* @throws IOException
*
*/
public static void createfile() throws IOException{
if (!filename.exists()) {
filename.createNewFile();
System.err.println("系统初始化完成!");
}
}
public static String readfile() throws IOException{
BufferedReader in = null;

String value = "";
FileReader r = new FileReader(path);
in = new BufferedReader(r);
String ss;
while((ss = in.readLine())!=null){
value = value + ss;
value += "\n";
}
in.close();
return value;

}
public static void writefile(String ss) throws IOException{
FileWriter w = new FileWriter(path);
BufferedWriter out = new BufferedWriter(w);
out.write(ss);
out.close();

}
public static void main(String []s) throws IOException{
createfile();
System.out.println("原文件内容:");
System.out.println("***********************************");
String tss = readfile();
System.out.println(tss);
System.out.println("***********************************");
Scanner input = new Scanner(System.in);
System.out.println("写入文件");
String ss = input.nextLine();
//ss += input.next();
System.out.println("写入的数据为:");
System.out.println(ss);
writefile(tss + ss);
tss = readfile();
System.out.println(tss);

}

}


guess.java

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

import java.util.Scanner;


public class guess {
public static int main(String [] args){
System.out.println("**************************************");
System.out.println("** 欢迎来到剪刀石头布游戏 **");
System.out.println("** 请选择规则 **");
System.out.println("** 1 三局两胜制 2 五局三胜制 **");
System.out.println("**************************************");
Scanner input = new Scanner(System.in);
int type = input.nextInt();
int sumtime = 0;
while(true){
if(type == 1){
sumtime = 3;
break;
}else if(type == 2){
sumtime = 5;
break;
}else {
System.out.println("输入错误!请重新输入:");
System.out.println("**************************************");
System.out.println("** 请选择规则 **");
System.out.println("** 1 三局两胜制 2 五局三胜制 **");
System.out.println("**************************************");
type = input.nextInt();
}
}
int flag = play(sumtime);
if(flag == 1){
return 1;
}else if(flag == 2){
return 2;
}else {
return 3;
}
}

private static int play(int i) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int stime = i;
int time=0;
int wintime=0;
int aiwin=0;
int you,ai;
int times=0;
System.out.println("***************************************");
System.out.println("** 请出拳: **");
System.out.println("** 1--> 剪刀 2-->石头 3-->布 **");
System.out.println("***************************************");
while(true){
System.out.println("第"+(times+1)+"局:\n");
String arr[]={"","剪刀","石头","布"};
you = input.nextInt();
while(true){
if(you <=3 && you >=1){
break;
}else{
System.out.println("输入有误,请重新输入!");
you = input.nextInt();
}
}
System.out.println("你出了:" + arr[you]);
ai = (int)(Math.random()*3+1);
System.out.println("机器人出了:" + arr[ai]);
if(you==ai){
wintime++;
aiwin++;
}else if((you==1&&ai==3)||(you==2&&ai==1)||(you==3&&ai==2)){
wintime++;
}else{
aiwin++;
}
times++;
if(times==stime){
break;
}
}
if(wintime==aiwin){
System.out.println("棋逢对手,平局!");
return 1;
}else if(wintime>aiwin){
System.out.println("恭喜你,你赢了!");
return 2;
}else{
System.out.println("很遗憾,你输了!");
return 3;
}
}
}

大码就是这个样子。。。。。

测试样例如下:

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
****************************************************
** 欢迎来到剪刀石头布游戏 **
** **
** 1.登录 2.注册 3.天梯 4.退出 **
****************************************************
1
请输入用户名:
test5
请输入密码:
pass5
登录成功!
欢迎用户test5登录剪刀石头布游戏!
****************************************************
** 欢迎来到剪刀石头布游戏 **
** 请选择规则 **
** 1 三局两胜制 2 五局三胜制 **
****************************************************
2
****************************************************
** 请出拳: **
** 1--> 剪刀 2-->石头 3-->布 **
****************************************************
第1局:

1
你出了:剪刀
机器人出了:布
第2局:

2
你出了:石头
机器人出了:剪刀
第3局:

3
你出了:布
机器人出了:剪刀
第4局:

1
你出了:剪刀
机器人出了:剪刀
第5局:

2
你出了:石头
机器人出了:布
棋逢对手,平局!
**************************************
** 是否再来一局? **
** **
** 1.再来一局 2.退出登录 **
**************************************
3
恭喜你中彩蛋啦!!!!!异常输入~积分加100~游戏退出!
**************************************************
** 欢迎来到剪刀石头布游戏 **
** **
** 1.登录 2.注册 3.天梯 4.退出 **
**************************************************
3
**************************************************
**************************************
** 天梯排行榜 **
** First Place:test5 106 **
** Second Place:test3 15 **
** Third Place:test7 9 **
**************************************
**************************************
**************************************************
** 欢迎来到剪刀石头布游戏 **
** **
** 1.登录 2.注册 3.天梯 4.退出 **
**************************************************
4
**********************************
**********************************
欢迎下次再来!谢谢!

下午

下午没什么事,老师让大家不看之前的代码,重新写一遍注册登录和游戏的程序。。。。。。。。。真是*了狗了,有个bug一直调不通,后来发现是个死循环,,,自己也是笨死了。。。基本就这样~

晚上

一如既往的写博客、看书、复习!加油↖(^ω^)↗


声明:
文章标题:实习生活第十二天
文章作者:h3h3da
文章链接:https://www.liucunzhan.com/p/54102954-7afa-4833-8891-1980740f315a
文章版权属本博主所有,有问题或者建议欢迎在下方评论。欢迎转载、引用,但请标明作者和原文地址,谢谢。


喜欢,就支持我一下吧~