您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關利用Java怎么實現一個幸運抽獎功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
代碼模塊:
User類:
package test1; public class User { private String userName; private String password; private int vipID; public User(String userName,String password,int vipID) { this.userName = userName; this.password = password; this.vipID = vipID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getVipID() { return vipID; } public void setVipID(int vipID) { this.vipID = vipID; } }
main函數:
package test1; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class TestUser { private static Scanner in; public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<User> user = new ArrayList<User>(); User u = null; String conf = null; String userName = null; String userPassword = null; String n; boolean flag = false; boolean flag0 = false; int[][] a = new int[2][5]; in = new Scanner(System.in); for(int i=0;i<2;i++) { for(int j=0;j<5;j++) { Random rand = new Random(); a[i][j] = rand .nextInt(9999) + 1000; } } do { System.out.println("****************歡迎進入獎客富翁系統*******************"); System.out.println(" 1、注冊"); System.out.println(" 2、登錄"); System.out.println(" 3、抽獎"); System.out.println("*******************************************************"); System.out.print("請選擇菜單:"); n = in.next(); switch(n) { //用戶注冊 case "1": { userName = null; userPassword = null; System.out.println("(獎客富翁系統 >注冊):"); System.out.print("用戶名:"); String name = in.next(); System.out.print("密 碼:"); String password = in.next(); System.out.print("確認密碼:"); String rePassword = in.next(); if(password.equals(rePassword)) { Random rand = new Random(); int randNumber = rand .nextInt(9000) + 1000; u = new User(name,password,randNumber); user.add(u); System.out.println("注冊成功,請記住您的會員卡號:"); System.out.println("用戶名 密碼 會員卡號"); System.out.println(u.getUserName()+" "+u.getPassword()+" "+u.getVipID()); } else { System.out.println("對不起,您輸入的密碼不一致!"); } System.out.print("\n繼續嗎?y/n:"); String confirm = null; boolean fir = false; while(fir == false) { confirm = in.next(); if(confirm.equals("Y")||confirm.equals("y")||confirm.equals("N")||confirm.equals("n")) { fir = true; break; } else { System.out.print("您的輸入為非法輸入,請重新輸入:"); fir = false; } } conf = confirm; if(conf.equals("n")||conf.equals("N")) { System.out.println("系統退出,謝謝使用!"); } } break; //用戶登錄 case "2": { String eName = null; String ePassword = null; userName = null; userPassword = null; boolean fla = false; int eVipID = 0; System.out.println("(獎客富翁系統 >登錄):"); while(fla == false){ System.out.print("用戶名:"); userName = in.next(); System.out.print("密 碼:"); userPassword = in.next(); if(user.isEmpty()) { System.out.println("您還沒有注冊,請先注冊!"); flag0 = true; fla = true; }else { for(User e: user) { if(userName.equals(e.getUserName())) { if(userName.equals(e.getUserName())&&userPassword.equals(e.getPassword())) { eName = e.getUserName(); ePassword = e.getPassword(); eVipID = e.getVipID(); fla = true; System.out.println("歡迎您:"+eName); }else { System.out.println("您輸入的賬號或密碼有誤,請重新輸入:"); } flag0 = true; break; } } if(flag0 == false) { System.out.println("您還沒有注冊,請先注冊!"); } } } if(flag0 == true) { System.out.print("繼續嗎?y/n:"); String confirm = in.next(); conf = confirm; if(conf.equals("Y")||conf.equals("y")) { u= new User(eName,ePassword,eVipID); } else if(conf.equals("n")||conf.equals("N")) { System.out.println("系統退出,謝謝使用!"); } }else { String confirm = null; boolean fir = false; while(fir == false) { confirm = in.next(); if(confirm.equals("Y")||confirm.equals("y")||confirm.equals("N")||confirm.equals("n")) { fir = true; break; } else { System.out.print("您的輸入為非法輸入,請重新輸入:"); } } conf = confirm; if(conf.equals("n")||conf.equals("N")) { System.out.println("系統退出,謝謝使用!"); } } } break; //抽獎 case "3": { System.out.println("(獎客富翁系統 >抽獎):"); if(u == null||(userName==null&&userPassword==null)){ System.out.println("您還沒有登錄,請重新登錄!"); System.out.print("繼續嗎?y/n:"); String confirm = null; boolean fir = false; while(fir == false) { confirm = in.next(); if(confirm.equals("Y")||confirm.equals("y")||confirm.equals("N")||confirm.equals("n")) { fir = true; break; } else { System.out.print("您的輸入為非法輸入,請重新輸入:"); fir = false; } } conf = confirm; if(conf.equals("n")||conf.equals("N")) System.out.println("系統退出,謝謝使用!"); } else{ System.out.println("歡迎"+u.getUserName()+"來到幸運抽獎!"); System.out.println("您的會員卡號為:"+u.getVipID()); for(int i=0;i<2;i++) { for(int j=0;j<5;j++) { System.out.print(a[i][j]+" "); if(u.getVipID() == a[i][j]) { flag = true; break; }else { flag=false; } } if(flag == true) break; } if(flag == true) { System.out.println("\n恭喜你成為本日的幸運會員!"); } else { System.out.println("\n抱歉,您不是本日的幸運會員!"); } System.out.print("繼續嗎?y/n:"); String confirm = null; boolean fir = false; while(fir == false) { confirm = in.next(); if(confirm.equals("Y")||confirm.equals("y")||confirm.equals("N")||confirm.equals("n")) { fir = true; break; } else { System.out.print("您的輸入為非法輸入,請重新輸入:"); fir = false; } } conf = confirm; if(conf.equals("n")||conf.equals("N")) System.out.println("系統退出,謝謝使用!"); } break; } default:{ System.out.println("您的輸入為非法輸入,請重新輸入:"); System.out.print("繼續嗎?y/n:"); String confirm = null; boolean fir = false; while(fir == false) { confirm = in.next(); if(confirm.equals("Y")||confirm.equals("y")||confirm.equals("N")||confirm.equals("n")) { fir = true; break; } else { System.out.print("您的輸入為非法輸入,請重新輸入:"); fir = false; } } conf = confirm; if(conf.equals("n")||conf.equals("N")) System.out.println("系統退出,謝謝使用!"); break; } } }while((conf.equals("Y")||conf.equals("y"))&&(!(n.equals("1"))||!(n.equals("2"))||!(n.equals("3")))); } }
看完上述內容,你們對利用Java怎么實現一個幸運抽獎功能有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。