User:
package com.javasm.bean; /** * @Author:Zxb * @Version:1.0 * @Date:2022/11/17-16:36 * @Since:jdk1.8 * @Description: */ public class User { public int userId;//卡号 public String userName;//用户名 public String userPwd;//密码 public int userLoginState;//登入状态 public User() { } public User(int userId, String userName, String userPwd, int userLoginState) { this.userId = userId; this.userName = userName; this.userPwd = userPwd; this.userLoginState = userLoginState; } public void show(){ System.out.println("会员卡号:" +this.userId+ "\t用户名:"+this.userName+"\t密码"+this.userPwd); } }
UserService:
package com.javasm.service; import com.javasm.bean.User; import java.util.Arrays; import java.util.Scanner; /** * @Author:Zxb * @Version:1.0 * @Date:2022/11/17-16:37 * @Since:jdk1.8 * @Description: */ public class UserService { static Scanner scanner = new Scanner(System.in); static User user = new User(); public static void startMenu() { boolean b = true; while (b) { System.out.println("****************欢迎使用XXX抽奖系统*******************"); System.out.println("\t\t1、注册"); System.out.println("\t\t2、登录"); System.out.println("\t\t3、抽奖"); System.out.println("\t\t0、退出"); System.out.println("请选择:"); int choose = scanner.nextInt(); switch (choose) { case 1: reg(); break; case 2: login(); break; case 3: draw(); break; case 0: System.out.println("[谢谢使用,系统已退出!!!]"); b = false; break; default: System.out.println("[选择错误,请重新选择!]"); break; } } } public static void reg() { System.out.println("抽奖系统>>>用户注册"); System.out.println("请输入注册用户名:"); user.userName = scanner.next(); System.out.println("请输入注册密码"); user.userPwd = scanner.next(); user.userId = (int) (Math.random() * 9000 + 1000); System.out.println("注册成功,请记住你的会员卡号!"); user.show(); } public static void login() { int i=3; boolean b=true; while(b){ i--; if(user.userName==null||user.userPwd==null){ System.out.println("登入失败!你还未注册!"); return; } System.out.println("抽奖系统>>>用户登录"); System.out.println("请输入用户名:"); String inputName=scanner.next(); System.out.println("请输入密码:"); String inputPwd=scanner.next(); if(inputName.equals(user.userName)&&inputPwd.equals(user.userPwd)){ System.out.println("登入成功!"); user.userLoginState=1; return; } if(i==0) { System.out.println("机会使用完毕,退出系统"); return; } System.out.println("用户名或密码错误!还剩"+i+"次机会"); System.out.println("是否继续?y/n"); String answer=scanner.next(); if("n".equals(answer)||"N".equals(answer)){ System.out.println("退出系统"); return; } } } public static void draw() { int[] luckyNum=new int[5]; if(user==null||user.userLoginState==0){ System.out.println("请先登入"); }else{ for(int i=0;i<5;i++) { luckyNum[i]=(int) (Math.random() * 9000 + 1000); } System.out.println("今日幸运数字:" + Arrays.toString(luckyNum)); for (int num : luckyNum) { if(num==user.userId) { System.out.println("恭喜你,成为今日幸运用户!"); } } System.out.println("抱歉,你不是幸运用户!"); } } }
UserTest:
package com.javasm.test; import com.javasm.service.UserService; /** * @Author:Zxb * @Version:1.0 * @Date:2022/11/17-16:36 * @Since:jdk1.8 * @Description: */ public class UserTest { public static void main(String[] args) { UserService.startMenu(); } }
标签:抽奖,int,System,面向对象,user,println,幸运,public,out From: https://www.cnblogs.com/19981206-zxb/p/16900472.html