首页 > 解决方案 > 1 秒计时器每秒激活多次

问题描述

试图用java制作一个cookie clicker风格的游戏。我是初学者,所以这很糟糕......这就是我所拥有的:

static double money = 50;
static double mps = 0; //money per second

public static void main(String[] args) throws InterruptedException{
    
    
    System.out.println("\n*You have $50*"
    + "\n*Use this money to make more money*"
    + "\n*Type \"stats\" To see what you can buy*");
    
    String statsString = "*Type Numbers To Buy, You Can Only Buy If You Have Enough Money!*"
            + "\n1 = $50 = 1 $/sec"
            + "\n2 = $100 = 2.2 $/sec"
            + "\n3 = $300 = 7.2 $/sec"
            + "\n4 = $1,200 = 31.2 $/sec"
            + "\n5 = $6,000 = 168 $/sec"
            + "\n6 = $36,000 = 1,080 $/sec"
            + "\n7 = $253,000 = 5,060 $/sec"
            + "\n8 = $2,024,000 = 68,816 $/sec"
            + "\n9 = $18,216,000 = 6 $/sec";
    
    
    while (true) {

    String input;
    try {

        Scanner stats = new  Scanner(System.in);

        input = stats.next();
        
        if(input.matches("stats")){ //if user inputs "stats", print stats
            System.out.println(statsString);
            
        } else if(input.matches("1") && money >= 50) { //if user inputs 1 and has 50+ money, buy one, subtract 50 from money and add 1 to money per second
       
            money = money - 50;
            mps = mps + 1;
            System.out.println(current money per second is " + mps);
            
        } else if(input.matches("2") && money >= 100) { //same as last comment, but different numbers
            
            money = money - 100;
            mps = mps + 2.20;
            System.out.println(current money per second is " + mps);
            
        } else if(input.matches("3") && money >= 300) { //same as last comment, but different numbers
            
            money = money - 300;
            mps = mps + 7.20;
            System.out.println(current money per second is " + mps);
            
        } else if(input.matches("4") && money >= 1200) { //same as last comment, but different numbers
            
            money = money - 1200;
            mps = mps + 31.20;
            System.out.println(current money per second is " + mps);
            
        } else if(input.matches("5") && money >= 6000) { //same as last comment, but different numbers
            
            money = money - 6000;
            mps = mps + 168;
            System.out.println(current money per second is " + mps);
            
        } else if(input.matches("6") && money >= 36000) { //same as last comment, but different numbers
            
            money = money - 36000;
            mps = mps + 1080;
            System.out.println(current money per second is " + mps);
            
        } else if(input.matches("7") && money >= 253000) { //same as last comment, but different numbers
            
            money = money - 253000;
            mps = mps +5060;
            System.out.println(current money per second is " + mps);
            
        } else if(input.matches("8") && money >= 2024000) { //same as last comment, but different numbers
            
            money = money - 2024000;
            mps = mps + 68816;
            System.out.println(current money per second is " + mps);
            
        } else if(input.matches("9") && money >= 18216000) { //same as last comment, but different numbers
            
            money = money - 18216000;
            mps = mps + 655776;
            System.out.println(current money per second is " + mps);
            
        } else{ //if none of the above inputs are input, say something is wrong, either not enough money or not a chosen valid input
            System.out.println("you don't have enough money for that!");
        }//end else

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }//end catch 
    
    
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            
            System.out.println("You have $" + money + "!");
            money = money + mps;
                            
        }//end public void run
    }, 0, 1000);//timer task
    }//end while true loop
}//end string

每次 else if 语句中的一个起作用时,它不是每秒输出钱,而是每秒输出两次,然后如果另一个 else if 语句起作用,每秒输出三个钱……本质上,每秒的钱比每次增加更多每次我按 1-9 时,第二次。

标签: java

解决方案


现在,每次用户购买商品时都会启动一个新的计时器任务。如果您将任务计时器移到 while 循环上方,则可以确保只有一个计时器正在执行。

    static double money = 50;
    static double mps = 0; //money per second

    public static void main(String[] args) throws InterruptedException{


        System.out.println("\n*You have $50*"
                + "\n*Use this money to make more money*"
                + "\n*Type \"stats\" To see what you can buy*");

        String statsString = "*Type Numbers To Buy, You Can Only Buy If You Have Enough Money!*"
                + "\n1 = $50 = 1 $/sec"
                + "\n2 = $100 = 2.2 $/sec"
                + "\n3 = $300 = 7.2 $/sec"
                + "\n4 = $1,200 = 31.2 $/sec"
                + "\n5 = $6,000 = 168 $/sec"
                + "\n6 = $36,000 = 1,080 $/sec"
                + "\n7 = $253,000 = 5,060 $/sec"
                + "\n8 = $2,024,000 = 68,816 $/sec"
                + "\n9 = $18,216,000 = 6 $/sec";


        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                System.out.println("You have $" + money + "!");
                money = money + mps;

            }//end public void run
        }, 0, 1000);//timer task
        while (true) {

            String input;
            try {

                Scanner stats = new  Scanner(System.in);

                input = stats.next();

                if(input.matches("stats")){ //if user inputs "stats", print stats
                    System.out.println(statsString);

                } else if(input.matches("1") && money >= 50) { //if user inputs 1 and has 50+ money, buy one, subtract 50 from money and add 1 to money per second

                    money = money - 50;
                    mps = mps + 1;
                    System.out.println("current money per second is " + mps);

                } else if(input.matches("2") && money >= 100) { //same as last comment, but different numbers

                    money = money - 100;
                    mps = mps + 2.20;
                    System.out.println("current money per second is " + mps);

                } else if(input.matches("3") && money >= 300) { //same as last comment, but different numbers

                    money = money - 300;
                    mps = mps + 7.20;
                    System.out.println("current money per second is " + mps);

                } else if(input.matches("4") && money >= 1200) { //same as last comment, but different numbers

                    money = money - 1200;
                    mps = mps + 31.20;
                    System.out.println("current money per second is " + mps);

                } else if(input.matches("5") && money >= 6000) { //same as last comment, but different numbers

                    money = money - 6000;
                    mps = mps + 168;
                    System.out.println("current money per second is " + mps);

                } else if(input.matches("6") && money >= 36000) { //same as last comment, but different numbers

                    money = money - 36000;
                    mps = mps + 1080;
                    System.out.println("current money per second is " + mps);

                } else if(input.matches("7") && money >= 253000) { //same as last comment, but different numbers

                    money = money - 253000;
                    mps = mps +5060;
                    System.out.println("current money per second is " + mps);

                } else if(input.matches("8") && money >= 2024000) { //same as last comment, but different numbers

                    money = money - 2024000;
                    mps = mps + 68816;
                    System.out.println("current money per second is " + mps);

                } else if(input.matches("9") && money >= 18216000) { //same as last comment, but different numbers

                    money = money - 18216000;
                    mps = mps + 655776;
                    System.out.println("current money per second is " + mps);

                } else{ //if none of the above inputs are input, say something is wrong, either not enough money or not a chosen valid input
                    System.out.println("you don't have enough money for that!");
                }//end else

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//end catch


        }//end while true loop
    }//end string

ps:在终端玩cookie clicker 很有趣。


推荐阅读