首页 > 解决方案 > 票务系统+定时器

问题描述

我目前正在开发一个离线 Jackpot 系统以获得乐趣,但我遇到了票务问题。基本上我希望它是(在我看来这是最好的方法),所以用户每输入 10 美元,你就会得到 1 张票。所以假设 Jason 存入 250 美元,然后他得到 1-25 票,Bob 存入 100 美元,然后他得到 26-36 票。等等等等然后最后它会选择一个随机数,然后它会选择一个在他的范围内有这个数字的玩家。这是当前代码:

public class Main {
private static int totalPlayers = 0;
private static int totalMoney = 0;
private static int playerDeposit = 0;
private static ArrayList<String> players = new ArrayList<String>();
private static ArrayList<Integer> botsMoney = new ArrayList<Integer>();
private static Timer timer;
private static Random random = new Random();
private static int playerMoney = 500;
private static String playerName;
private static double playerChance;
public static void main(String[] args) {
    onUserEnter();
}

public static boolean onUserEnter(){
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter your name:");
    playerName = scan.nextLine();
    System.out.println("Money: 500$");
    System.out.println("How much would you like to deposit?");
    try{
        playerDeposit = scan.nextInt();
    }catch(NumberFormatException ex) {
        System.out.println(playerDeposit + " is not a proper number!");
        playerDeposit = 0;
        return false;
    }
    if(playerDeposit <= 0) {
        System.out.println("You can't deposit " + playerDeposit + "$");
        return false;
    }else if(playerDeposit > playerMoney){
        System.out.println("You can't deposit more money than what you have!");
        return false;
    }
    playerMoney -= playerDeposit;
    System.out.println("You now have: " + playerMoney + "$");
    players.add(playerName);
    totalPlayers = players.size();
    totalMoney += playerDeposit;
    System.out.println(totalPlayers + " Players: " + players);
    System.out.println("Total Pot: " + totalMoney + ", Your chances: 100%");
    TimerTask tasknew = new TimerTask() {
        @Override
        public void run() {

        }
    };
    timer = new Timer();
    timer.schedule(tasknew, 20000);
    botJoin();
    return true;
}
public static boolean botJoin(){
    botsMoney.add(5355);
    int x = 0;
    while(true){
        int willBotJoin = random.nextInt(1000000000);
        if(willBotJoin <= 2){
            if(players.size() >= 10) {
            break;
            }
            int moneyBotDeposits = random.nextInt(1100);
            botsMoney.add(moneyBotDeposits);
            double tickets = (double) moneyBotDeposits / 10;

            x++;
            players.add("Bob #" + x);
            totalPlayers = players.size();
            totalMoney += moneyBotDeposits;
            System.out.println(totalPlayers + " Players: " + players);
            playerChance = ((double) playerDeposit / totalMoney) * 100;
            System.out.println("Total Pot: " + totalMoney + "$, Your chances: " + playerChance + "%");
        }
    }
    botsPrecentage();
    return true;
}
public static boolean botsPrecentage(){
    for (int i = 1; i <= 9; i++){
        double botPrecent = ((double) botsMoney.get(i) / totalMoney) * 100;
        String botName = players.get(i);
        System.out.println(botName + " has " + botPrecent + "%");
    }
    System.out.println(playerName + " has " + playerChance + "%");
    return true;
}

我也尝试设置一个我不明白的计时器,我在网上查了计时器,但我就是看不懂解释。

标签: javatimer

解决方案


推荐阅读