java實現(xiàn)幸運抽獎功能
本文實例為大家分享了java實現(xiàn)幸運抽獎功能的具體代碼,供大家參考,具體內(nèi)容如下
本系統(tǒng)較為簡單,未使用是什么多的算法,也未添加保存文件讀取文件功能,
1、任務(wù)
模擬注冊登錄幸運抽獎全過程
2、主要功能:
1.注冊
2.登錄
3.退出登錄
4.抽獎
5.退出系統(tǒng)
首頁:
1.輸出菜單
2.選擇菜單編號
3.如果編號選擇錯誤,輸出“您的輸入有誤!”
注冊:
1.輸入用戶名和密碼,系統(tǒng)產(chǎn)生4位隨機數(shù)作為卡號。
2.注冊成功,輸出用戶信息
登錄:
1.輸入注冊時的用戶名和密碼,登錄成功,系統(tǒng)提示登陸成功。
2.如果用戶名和密碼輸入錯誤,提示用戶繼續(xù)輸入。
退出登錄:
1.若用戶已登錄則退出登錄
2.若用戶未登錄則顯示您未登錄,
抽獎:
1.輸入會員卡號,系統(tǒng)生成5個4位隨機數(shù)作為幸運數(shù)字
2.如果會員卡號是其中之一,則成為本日幸運會員;否則不是幸運會員
退出系統(tǒng):
若用戶想結(jié)束對本系統(tǒng)的使用可退出系統(tǒng),結(jié)束程序。
代碼實現(xiàn):
import java.util.Scanner; ? class User{//用戶 ? ? String name; ? ? String password; ? ? int cardid; ? ? User(String name,String password){ ? ? ? ? this.name=name; ? ? ? ? this.password=password; ? ? ? ? cardid=(int)(Math.random()*9000+1000); ? ? } ? ? String getName(){ ? ? ? ? return name; ? ? } ? ? String getPassword(){ ? ? ? ? return password; ? ? } ? ? int getCardid(){ ? ? ? ? return cardid; ? ? } } public class Lottery {//抽獎系統(tǒng) ? ? ? public static void main(String[] args) {//main方法 ? ? ? ? User[]user=new User[10]; ? ? ? ? int total=0;//注冊人數(shù) ? ? ? ? int j=0;//是否退出系統(tǒng) ? ? ? ? int x=0;//登錄狀態(tài),默認未登錄 ? ? ? ? int y=-1;//當(dāng)前登錄用戶元素 ? ? ? ? do { ? ? ? ? ? ? System.out.println("*****歡迎進入幸運抽獎系統(tǒng)*****"); ? ? ? ? ? ? System.out.println("\t1、注冊"); ? ? ? ? ? ? System.out.println("\t2、登錄"); ? ? ? ? ? ? System.out.println("\t3、退出登錄"); ? ? ? ? ? ? System.out.println("\t4、抽獎"); ? ? ? ? ? ? System.out.println("\t5、退出系統(tǒng)"); ? ? ? ? ? ? System.out.print("\t請選擇:"); ? ? ? ? ? ? int choice; ? ? ? ? ? ? Scanner reader=new Scanner(System.in); ? ? ? ? ? ? choice=reader.nextInt(); ? ? ? ? ? ? ? switch (choice){ ? ? ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? ? ? if (x==0) ? ? ? ? ? ? ? ? ? ? total = getTotal(user, total); ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("您正在登錄中,請先退出登錄再注冊!\n"); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 2: ? ? ? ? ? ? ? ? ? ? if (x==0) { ? ? ? ? ? ? ? ? ? ? ? ? int i = 0; ? ? ? ? ? ? ? ? ? ? ? ? do { ? ? ? ? ? ? ? ? ? ? ? ? ? ? y = toLogin(user, total); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (y==-1) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("您的輸入有誤,請重新輸入!\n"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? i = 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? x = 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? i=0; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } while (i == 1); ? ? ? ? ? ? ? ? ? ? }else ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("您正在登錄中!\n"); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 3: ? ? ? ? ? ? ? ? ? ? if (x==1){ ? ? ? ? ? ? ? ? ? ? ? ? x=0; ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("退出登錄成功!\n"); ? ? ? ? ? ? ? ? ? ? }else ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("您未登錄,請先登錄!\n"); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 4: ? ? ? ? ? ? ? ? ? ? if (x==1){ ? ? ? ? ? ? ? ? ? ? ? ? toLottery(user, y); ? ? ? ? ? ? ? ? ? ? }else ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("您未登錄,請先登錄!\n"); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 5: ? ? ? ? ? ? ? ? ? ? j=1; ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? System.out.println("您的輸出有誤,請重新輸入!\n"); ? ? ? ? ? ? } ? ? ? ? }while(j!=1); ? ? } ? ? ? private static void toLottery(User[] user, int y) {//抽獎方法 ? ? ? ? int j=0; ? ? ? ? System.out.println("本日幸運會員卡號為:"); ? ? ? ? int cardid[]=new int[5]; ? ? ? ? cardid[0]=(int)(Math.random()*9000+1000); ? ? ? ? cardid[1]=(int)(Math.random()*9000+1000); ? ? ? ? cardid[2]=(int)(Math.random()*9000+1000); ? ? ? ? cardid[3]=(int)(Math.random()*9000+1000); ? ? ? ? cardid[4]=(int)(Math.random()*9000+1000); ? ? ? ? for (int i=0;i<5;i++){ ? ? ? ? ? ? System.out.print(cardid[i]+" ?"); ? ? ? ? ? ? if(user[y].getCardid()==cardid[i]){ ? ? ? ? ? ? ? ? j=1; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? System.out.println("\n您的會員卡號為:\n"+user[y].getCardid()); ? ? ? ? if (j==1) ? ? ? ? ? ? System.out.println("恭喜您,成為本日的幸運會員!\n"); ? ? ? ? else ? ? ? ? ? ? System.out.println("很遺憾,您不是本日幸運會員!\n"); ? ? } ? ? ? private static int toLogin(User[] user, int total) {//登錄方法 ? ? ? ? Scanner reader=new Scanner(System.in); ? ? ? ? System.out.print("請輸入您的用戶名:"); ? ? ? ? String name= reader.nextLine(); ? ? ? ? System.out.print("請輸入您的密碼:"); ? ? ? ? String password= reader.nextLine(); ? ? ? ? int j=-1; ? ? ? ? for (int i = 0; i< total; i++) { ? ? ? ? ? ? if (name.equals(user[i].getName())) { ? ? ? ? ? ? ? ? if (password.equals(user[i].getPassword())) { ? ? ? ? ? ? ? ? ? ? System.out.println("登陸成功!"); ? ? ? ? ? ? ? ? ? ? System.out.println("用戶名:"+name+"\n密碼:"+password+"\n會員號:"+user[i].getCardid()+"\n"); ? ? ? ? ? ? ? ? ? ? j=i; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return j; ? ? } ? ? private static int getTotal(User[] user, int total) {//注冊方法 ? ? ? ? Scanner reader=new Scanner(System.in); ? ? ? ? System.out.print("請輸入您的用戶名:"); ? ? ? ? String name= reader.nextLine(); ? ? ? ? System.out.print("請輸入您的密碼:"); ? ? ? ? String password= reader.nextLine(); ? ? ? ? for (int i=0;i<total;i++) { ? ? ? ? ? ? if (name.equals(user[i].getName())) { ? ? ? ? ? ? ? ? System.out.println("用戶名已存在,請重新輸入!\n"); ? ? ? ? ? ? ? ? return total; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? user[total]=new User(name,password); ? ? ? ? System.out.println("用戶名:"+name+"\n密碼:"+password+"\n會員號:"+user[total].getCardid()+"\n"); ? ? ? ? total++; ? ? ? ? return total; ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PowerJob的QueryConvertUtils工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的QueryConvertUtils工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01eclipse里沒有“Dynamic Web Project“這個選項的問題解決
本文主要介紹了eclipse里沒有“Dynamic Web Project“這個選項的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問404
在本篇文章里小編給大家整理的是一篇關(guān)于如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問404相關(guān)文章,需要的朋友們學(xué)習(xí)下。2019-11-11java在linux本地執(zhí)行shell命令的實現(xiàn)方法
本文主要介紹了java在linux本地執(zhí)行shell命令的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Go反射底層原理及數(shù)據(jù)結(jié)構(gòu)解析
這篇文章主要介紹了Go反射底層原理及數(shù)據(jù)結(jié)構(gòu)解析,反射的實現(xiàn)和interface的組成很相似,都是由“類型”和“數(shù)據(jù)值”構(gòu)成,下面小編分享更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-06-06