1)登录:
2)注册:
3)抽奖:
注意事项
运行代码之前务必在user.txt中创建五个以上的用户。
完整代码importjava.util.Scanner;importjava.util.ArrayList;importjava.io.*;importjava.util.StringTokenizer;publicclassDos{staticbooleanlogined=false;publicstaticvoidmain(String[]args){Useruser=newUser();intk=0;while((k=Main(user))>=1&&k<5){switch(k){case1:System.out.print((k=user.login(user))==-1?”此用户不存在!\n”:””);System.out.print((k==-2)?”===<<警告>>用户:[” user.userName “]已处于登录状态,无需重复登录!\n”:””);break;case2:user.regist();break;case3:user.getLuckly();break;default:System.exit(0);}}}staticintMain(Useruser){System.out.println(“**********************************************”);System.out.println(“********************主菜单********************”);System.out.println(“**********************************************”);System.out.println(“******<1>登录******”);System.out.println(“******<2>注册******”);System.out.println(“******<3>抽奖******”);System.out.println(“******<4>退出******”);System.out.println(“**********************************************”);System.out.println(“==============================================”);System.out.println(logined?”-[已登录]-(1)用户名:” user.userName “(2)用户账号:” user.userId:”-[未登录]-(1)用户名:NaN(2)用户账号:NaN”);System.out.println(“==============================================”);System.out.print(“###===>请输入您的选择:”);return(newScanner(System.in)).nextInt();}}publicclassUser{StringuserName,userId,userPwd;publicUser(){}publicUser(StringuserName,StringuserId,StringuserPwd){this.userName=userName;this.userId=userId;this.userPwd=userPwd;}publicStringgetUserName(){returnuserName;}publicvoidsetUserName(StringuserName){if(!userName.equals(“”)){ArrayList<String>temp;this.userId=(temp=(newoperatorFile(this.userName=userName)).getUserMess(0)).get(1);this.userPwd=temp.get(2);}}publicStringgetUserId(){returnuserId;}publicStringsetUserId(){StringuserId=””;while((newoperatorFile(userId=String.valueOf((int)(Math.random()*9000 1000)))).getUserMess(1).size()>0){}return(this.userId=userId);}publicStringgetUserPwd(){returnuserPwd;}publicintlogin(Useru){intinputTimes=3;Scannerscanner=newScanner(System.in);operatorFilegetUserMessage=newoperatorFile();System.out.print(“======>请输入您的用户名:”);StringuName=””;getUserMessage.setUser(uName=scanner.nextLine());ArrayList<String>userMess=getUserMessage.getUserMess(0);if(userMess.size()<1)return-1;//返回-1表示用户不存在if(uName.equals(userName))return-2;//返回-2表示用户重复登录System.out.print(“======>请输入您的登录密码:”);while(!scanner.next().equals(userMess.get(2))&&inputTimes>0)System.out.print(“===>密码输入错误!” ((–inputTimes)>0?”您还剩” inputTimes “次机会!”:”三次机会已经用完了!输入任意退出”));System.out.println(inputTimes>0?”==>登录成功!您本次输入密码” (4-inputTimes) “次!”:”==>登录失败!”);setUserName(inputTimes>0?uName:””);Dos.logined=inputTimes>0?true:false;return0;}publicvoidregist(){Useru=newUser();Scannerscanner=newScanner(System.in);System.out.print(“===>请输入新的用户名:”);Stringname;while(newoperatorFile(name=scanner.nextLine()).getUserMess(0).size()>0)System.out.print(“已存在此用户,注册失败!\n===>请重新输入新的用户名:”);System.out.print(“======>请设置您的(六位数字)登录密码:”);Stringregex=”[0-9]{6}”,pwd;while(!(pwd=scanner.nextLine()).matches(regex))System.out.print(“==>密码格式不正确,请重新设置您的(六位数字)登录密码:”);System.out.println(“已为用户:” (u.userName=name) “生成唯一ID:” (u.userPwd=pwd));(newoperatorFile()).writeUserMess(u);System.out.println(“=======>注册成功!”);}publicstaticHashMap<String,String>lucklyUsers=newHashMap<>();publicvoidgetLuckly(){if(!Dos.logined){System.out.println(“===>警告:没有用户登录,无法抽奖!”);return;}while(lucklyUsers.size()<5){Stringid=””;ArrayList<String>u;while((u=(newoperatorFile(id=String.valueOf((int)(Math.random()*9000 1000)))).getUserMess(1)).size()<1){}lucklyUsers.put(u.get(1),u.get(0));}Iteratoriterator=lucklyUsers.entrySet().iterator();intno=1;booleanLUCKLY=false;System.out.println(“====>恭喜以下用户获得幸运称号:”);while(iterator.hasNext()){Map.Entryentry=(Map.Entry)iterator.next();System.out.println(“幸运用户[” (no ) “]用户名:” entry.getValue() “用户编号:” entry.getKey());LUCKLY=entry.getKey().equals(this.userId)?true:LUCKLY;}System.out.println(LUCKLY?”=========>恭喜您在本次抽奖中获得幸运称号!”:”=========>很遗憾,今日您未获奖!-_-!”);}publicStringtoString(){returnthis.userName “” this.userId “” this.userPwd;}}publicclassoperatorFile{Stringuser;publicvoidsetUser(Stringuser){this.user=user;}publicoperatorFile(Stringuser){this.user=user;}publicoperatorFile(){}publicArrayList<String>getUserMess(intindex){ArrayList<String>temp=newArrayList<String>();Filefile=newFile(“user.txt”);Stringline=””;try{BufferedReaderbr=newBufferedReader(newFileReader(file));while((line=br.readLine())!=null&&line!=”\n”){temp.clear();StringTokenizersk=newStringTokenizer(line);while(sk.hasMoreTokens()){temp.add(sk.nextToken());}if(temp.get(index).equals(this.user))break;}}catch(IOExceptione){}return(line==null)?newArrayList<String>():temp;}publicvoidwriteUserMess(Useru){try{BufferedWriterbw=newBufferedWriter(newFileWriter(newFile(“user.txt”),true));bw.write(u.toString() “\n”);bw.close();}catch(IOExceptione){}}}
不花钱的IDEA 2020.2.4 最新激活教程
2020-12-01
动态代理、静态代理(易懂有栗子)
2020-11-30
再也不怕误删文件了!微软发布免费的文件恢复工具(干货)
2020-12-02
【建议收藏】TCP协议灵魂12问,总会用得到
2020-12-03
用了这些建议后系统性能足足提升了 10 倍!!!
2020-12-05
END