JAVA 超简单方法设计抽奖系统

Game.java 测试类类

import java.util.Scanner;

public class Game {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        UI.disPlay();   
        while (true) {
            int num = scanner.nextInt();
            if(Select.choce()) {
                if (num == 1) {
                    UI.sign();
                }else if (num == 2) {
                    UI.login();
                }else if (num == 3) {
                    UI.luck();
                    System.out.println("抽奖结束,选择是否继续?");
                    Select.choce();
                }
            }
                UI.disPlay();
        }
    }
}

Login.java 登陆系统类

import java.util.Scanner;

public class Login {
    static Register register = new Register();
    public static void login() {    
        Scanner scanner = new Scanner(System.in);   
        int count = 0;
        //判断是否注册
        if (register.getUsename() == null) {
            System.out.println("请先注册");
            UI.sign();
        }
        System.out.println("[抽奖系统 > 登陆]");

    while (true) {  
        System.out.println("请输入用户名:");
        String usename = scanner.nextLine();
        System.out.println("请输入密码:");
        String temNum = scanner.nextLine();
        int password = Integer.parseInt(temNum);
        //核对用户名和密码  可以只核对用户名
        if (usename.equals(register.getUsename()) && password == register.getPassword()) {
            System.out.println("欢迎您" + register.getUsename());
            break;
        }else {
            System.out.println("用户名或密码错误,请重新输入!");  
            count++;                
            if (count == 3) {
                System.out.println("对不起,三次机会已用完,请明天再来");
                System.exit(0);
            }
        }
    }

}

}

Lottery.java 抽奖系统类

import java.util.Scanner;

public class Lottery {

    public static void luckNum() {
    //判断是否注册
        Register register = new Register();
        if (register.getUsename() == null) {
            System.out.println("请先注册");
            UI.sign();
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("[抽奖系统 > 抽奖]");
        System.out.println("请输入您的卡号:");
        int luckNum = scanner.nextInt();
        // 此次可优化为循环
        int num1 = (int) (Math.random()*9000 + 1000);
        int num2 = (int) (Math.random()*9000 + 1000);
        int num3 = (int) (Math.random()*9000 + 1000);
        int num4 = (int) (Math.random()*9000 + 1000);
        int num5 = (int) (Math.random()*9000 + 1000);
        System.out.println("今日幸运数字是:" + num1 + ", " + num2 + ", " + num3 + ", " + num4 + ", " + num5 + ".");
        if ( luckNum == num1 || luckNum == num2 || luckNum == num3|| luckNum == num4 || luckNum == num5 ) {         
            System.out.println("恭喜你中奖!");
        }else {
            System.out.println("很遗憾你没有中奖,请下次再来。");

        }
    }
}

Register.java 注册信息类

public class Register {
    private static String usename;
    private static int password;
    private static int id;

    public String getUsename() {
        return usename;
    }
    public void setUsename(String usename) {
        this.usename = usename;
    }
    public int getPassword() {
        return password;
    }
    public void setPassword(int password) {
        this.password = password;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }   
}

Select.java 选择类

import java.util.Scanner;

public class Select {
    public static boolean choce() {
        System.out.println("是否继续 输入Y/N");
        Scanner scanner = new Scanner(System.in);
        String st = scanner.nextLine();
        //判断 用户输入
        if (st.equals("Y") || st.equals("y")) {
            return true;
        }else  {
            System.out.println("欢迎下次使用");
            System.exit(0);
            return false;       
        }       
    }
}

UI.java 界面类

import java.util.Scanner;

public class UI {
    static Register register = new Register();
    static Scanner scanner = new Scanner(System.in);
    //创建界面
    public static void disPlay () {
        System.out.println("*****欢迎来到抽奖系统*****");
        System.out.println("         "+"1.注册");
        System.out.println("         "+"2.登陆");
        System.out.println("         "+"3.抽奖");
        System.out.println("***********************");
        System.out.println("请选择菜单:");
    }
    public static void sign() {
        //注册
        System.out.println("[抽奖系统 > 注册]");
        System.out.println("请填写个人注册信息");
        System.out.println("用户名:");

        String usename = scanner.nextLine();
        register.setUsename(usename);
        System.out.println("密码:");
        String num = scanner.nextLine();
        int password = Integer.parseInt(num);
        register.setPassword(password);
        int cardId = (int) (Math.random()*9000 + 1000);
        register.setId(cardId);

        System.out.println("注册成功,请记住你的会员卡号");
        System.out.println("用户名:" + register.getUsename());
        System.out.println("密码:" + register.getPassword());
        System.out.println("卡号:" + register.getId());
    }
    //登陆
    public static void login() {    
        Login.login();
    }
        //抽奖
    public static void luck() {
        Lottery.luckNum();
    }
}

你可能感兴趣的:(java)