您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天就跟大家聊聊有关利用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。