本文实例为大家分享了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")))); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。