<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          用Java實現(xiàn)一個抽獎系統(tǒng)(附完整代碼)

          共 2549字,需瀏覽 6分鐘

           ·

          2021-01-05 22:11

          作者:四原色

          來源:https://blog.csdn.net/qq_44140450

          需求分析

          1)實現(xiàn)三個基本功能:登錄、注冊、抽獎。
          2)登錄:用戶輸入賬號密碼進行登錄,輸入賬號后會匹配已注冊的用戶,若輸入用戶不存在則退出,密碼有三次輸入機會,登錄成功后主界面會顯示已登錄用戶的賬號信息。
          3)注冊:用戶首先輸入賬號名稱,系統(tǒng)查詢此名稱是否存在,如存在則請求用戶換一個名稱,否則進入密碼輸入,密碼要求6位數(shù)字字符串,注冊成功后,系統(tǒng)隨機分配一個與已有用戶不重復的四位數(shù)字id編號。
          4)抽獎:功能實現(xiàn)前提:需有用戶處于登錄狀態(tài)。該前提滿足時,系統(tǒng)從已存在用戶中隨機抽取5位不同的用戶標記為幸運用戶,并判斷正在登錄狀態(tài)的用戶是否被抽中。
          5)數(shù)據(jù)存儲:采用文件系統(tǒng),導入java.io.*包,
          6)數(shù)據(jù)結構:登錄用戶信息保存于ArrayList,幸運用戶編號和id保存于長度為5的HasMap 其中id為Key,name為Value。

          實現(xiàn)結果

          1)登錄:

          2)注冊:

          3)抽獎:

          注意事項

          運行代碼之前務必在user.txt中創(chuàng)建五個以上的用戶。


          完整代碼

          import?java.util.Scanner;
          import?java.util.ArrayList;
          import?java.io.*;
          import?java.util.StringTokenizer;

          public?class?Dos?{
          ????static?boolean?logined=false;
          ????public?static?void?main(String[]?args)?{
          ????????User?user=new?User();
          ????????int?k=0;
          ????????while(?(k=Main(user))>=1&&k<5){
          ????????????switch?(k){
          ????????????????case?1:
          ????????????????????System.out.print((k=user.login(user))==-1?"此用戶不存在!\n":"");
          ????????????????????System.out.print((k==-2)?"===<<警告>>用戶:["+user.userName+"]已處于登錄狀態(tài),無需重復登錄!\n":"");
          ????????????????????break;
          ????????????????case?2:
          ????????????????????user.regist();
          ????????????????????break;
          ????????????????case?3:
          ????????????????????user.getLuckly();
          ????????????????????break;
          ????????????????default:System.exit(0);
          ????????????}
          ????????}
          ????}
          ????static?int?Main(User?user){
          ????????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?(new?Scanner(System.in)).nextInt();
          ????}
          }
          public?class?User{
          ????String?userName,userId,userPwd;
          ????public?User(){}
          ????public?User(String?userName,?String?userId,?String?userPwd)?{
          ????????this.userName?=?userName;
          ????????this.userId?=?userId;
          ????????this.userPwd?=?userPwd;
          ????}
          ????public?String?getUserName()?{
          ????????return?userName;
          ????}
          ????public?void?setUserName(String?userName)?{
          ????????if(!userName.equals(""))?{
          ????????????ArrayList?temp;
          ????????????this.userId=(temp=(new?operatorFile(this.userName?=?userName)).getUserMess(0)).get(1);
          ????????????this.userPwd=temp.get(2);
          ????????}
          ????}
          ????public?String?getUserId()?{
          ????????return?userId;
          ????}
          ????public?String?setUserId()?{
          ????????String?userId="";
          ????????while((new?operatorFile(userId=String.valueOf?((int)?(Math.random()*9000+1000)))).getUserMess(1).size()>0){?}
          ????????return?(this.userId?=?userId);
          ????}
          ????public?String?getUserPwd()?{
          ????????return?userPwd;
          ????}
          ????public?int?login(User?u)?{
          ????????int?inputTimes=3;
          ????????Scanner?scanner=new?Scanner(System.in);
          ????????operatorFile?getUserMessage=new?operatorFile();
          ????????System.out.print("======>請輸入您的用戶名:");
          ????????String?uName="";
          ????????getUserMessage.setUser(uName=scanner.nextLine());
          ????????ArrayList?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;
          ????????return?0;
          ????}
          ????public?void?regist()?{
          ????????User?u=new?User();
          ????????Scanner?scanner=new?Scanner(System.in);
          ????????System.out.print("===>請輸入新的用戶名:");
          ????????String?name;
          ????????while(new?operatorFile(name=scanner.nextLine()).getUserMess(0).size()?>?0)
          ????????????System.out.print("已存在此用戶,注冊失敗!\n===>請重新輸入新的用戶名:");
          ????????System.out.print("======>請設置您的(六位數(shù)字)登錄密碼:");
          ????????String?regex?=?"[0-9]{6}",?pwd;
          ????????while?(!(pwd?=?scanner.nextLine()).matches(regex))
          ????????????System.out.print("==>密碼格式不正確,請重新設置您的(六位數(shù)字)登錄密碼:");
          ????????System.out.println("已為用戶:"+(u.userName=name)+"?生成唯一ID:?"+(u.userPwd=pwd));
          ????????(new?operatorFile()).writeUserMess(u);
          ????????System.out.println("=======>注冊成功!");
          ????}
          ????public?static?HashMap?lucklyUsers=new?HashMap<>();
          ????public?void?getLuckly()?{
          ????????if?(!Dos.logined)???{
          ????????????System.out.println("===>警告:沒有用戶登錄,無法抽獎!");
          ????????????return?;
          ????????}
          ????????while(lucklyUsers.size()<5)
          ????????{
          ????????????String?id="";
          ????????????ArrayList?u;
          ????????????while((u=(new?operatorFile(id=String.valueOf?((int)?(Math.random()*9000+1000)))).getUserMess(1)).size()<1){?}
          ????????????lucklyUsers.put(u.get(1),u.get(0));
          ????????}
          ????????Iterator?iterator=lucklyUsers.entrySet().iterator();
          ????????int?no=1;
          ????????boolean?LUCKLY=false;
          ????????System.out.println("====>恭喜以下用戶獲得幸運稱號:");
          ????????while(iterator.hasNext()){
          ????????????Map.Entry?entry=(Map.Entry)?iterator.next();
          ????????????System.out.println("幸運用戶["+(no++)+"]??用戶名:"+entry.getValue()+"???用戶編號:"+entry.getKey());
          ????????????LUCKLY?=?entry.getKey().equals(this.userId)???true?:?LUCKLY;
          ????????}
          ????????System.out.println(LUCKLY?"=========>恭喜您在本次抽獎中獲得幸運稱號!":"=========>很遺憾,今日您未獲獎?!-_-!");
          ????}
          ????public?String?toString(){
          ????????return?this.userName+"?"+this.userId+"?"+this.userPwd;
          ????}
          }
          public?class?operatorFile?{
          ????String?user;
          ????public?void?setUser(String?user)?{
          ????????this.user?=?user;
          ????}
          ????public?operatorFile(String?user)?{
          ????????this.user?=?user;
          ????}
          ????public?operatorFile()?{?}
          ????public?ArrayList?getUserMess(int?index){
          ????????ArrayList?temp=new?ArrayList();
          ????????File?file=new?File("user.txt");
          ????????String?line="";
          ????????try{
          ????????????BufferedReader?br=new?BufferedReader(new?FileReader(file));
          ????????????while?((line?=?br.readLine())!=null?&&?line!="\n"){
          ????????????????temp.clear();
          ????????????????StringTokenizer?sk=new?StringTokenizer(line);
          ????????????????while?(sk.hasMoreTokens())?{
          ????????????????????temp.add(sk.nextToken());
          ????????????????}
          ????????????????if?(temp.get(index).equals(this.user))??break;
          ????????????}
          ????????}
          ????????catch(IOException?e){}
          ????????return?(line==null)?new?ArrayList():temp;
          ????}
          ????public?void?writeUserMess(User?u){
          ????????try{
          ????????????BufferedWriter?bw=new?BufferedWriter(new?FileWriter(new?File("user.txt"),true));
          ????????????bw.write(u.toString()+"\n");
          ????????????bw.close();
          ????????}
          ????????catch?(IOException?e){?}
          ????}
          }

          題外話:推薦一個GitHub項目,這個 GitHub 整理了上百本常用技術PDF,絕大部分核心的技術書籍都可以在這里找到,GitHub地址:https://github.com/gsjqwyl/awesome-ebook(電腦打開體驗更好),地址閱讀原文直達。麻煩打個給個Star,持續(xù)更新中...


          ---END---

          文末福利




          瀏覽 71
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产熟妇 码视频户外直播 | 翔田千里在线视频 | 97福利视频 | 青娱乐一区二区三区 | 久久少妇久久 |