呼叫时额外输出

时间:2013-11-14 16:51:49

标签: java timer duplicate-removal

好的我已经被要求进行一项反射游戏的任务,这将允许用户启动和停止计时器,然后在结束时显示他们的时间。目前它主要是工作,除了目前有一个问题,它会说push 1启动,因为它打算和启动我的计时器,但然后它将显示此消息再次显示秒数,这不是预期的。因此,如果有人可以帮助或者指出可能导致这种情况的事情,那将不胜感激

public class TimerTest {

    static int attempts = 4;
    static Timer timer = new Timer();
    static int controler;
    static int seconds = 1;
    static Scanner keyboard = new Scanner(System.in);
    static int time1, time2, time3;

    public static void run() {

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                if (controler == 1) {
                    System.out.println(seconds++ + " seconds");
                }
            } // closed the running of timer

        }, 0, 1000);
        // closed timer

    }

    public static void main(String args[]) {

        System.out.println("\t\t\tthe rules\n");
        System.out.println("1) press 1 then enter to start the game");
        System.out.println("2) press 2 then enter as soon as you can to stop the timer");
        System.out.println("3)only enter the specified numbers none other");
        System.out.println("4) numbers only no letters\n\n");

        do {

            System.out.println("\t\t\tplease push 1 to start");
            controler = keyboard.nextInt();

            if (controler == 1 && attempts != 0) {

                run();
                attempts--;

            } else if (controler == 2 && attempts == 3) {

                System.out.println("stopped ");
                controler = controler + 1;
                System.out.println("your time is " + seconds);
                time1 = seconds;
                seconds = 0;

            } else if (controler == 2 && attempts == 2) {

                System.out.println("stopped ");
                controler = controler + 1;
                System.out.println("go 2 " + seconds);
                time2 = seconds;
                seconds = 0;

            } else if (controler == 2 && attempts == 1) {

                System.out.println("stopped ");
                controler = controler + 1;
                time3 = seconds;
                System.out.println("final go " + seconds);
                seconds = 0;
                attempts = 0;

            }

            System.out.println("\nyou have " + attempts + " attempts remaining\n");

        } while (attempts != 0);

        if (time1 > time2 && time1 > time3) {
            System.out.println("your first go was slowest with a time of " + time1 + " seconds");
        } else if (time2 > time1 && time2 > time3) {
            System.out.println("your second go was slowest with a time of " + time2 + " seconds");
        } else if (time3 > time1 && time3 > time2) {
            System.out.println("your final go was slowest with a time of " + time3 + " seconds");
        } else {
            System.out.println("STOP FUCKING UP MY PROGRAM JAVA");
        }

        if (attempts == 0) {
            System.out.println("\ngame over, thanks for playing and please try again");
            System.exit(0);
        }

    }

}

2 个答案:

答案 0 :(得分:0)

移动

System.out.println("\t\t\tplease push 1 to start");

在你的do-while循环之外。否则它将在每次迭代时打印,这似乎不止一次。

答案 1 :(得分:0)

假设您想尝试迭代while循环(并显示您的消息)次数:

将您的陈述置于条件中,仅在控制器不是1时打印。

if (controler != 1) {
    System.out.println("\t\t\tplease push 1 to start");
}
controler = keyboard.nextInt();

另外:请注意,您需要以某种方式停止计时器。可能你想保留对它的引用,并在按下'2'时调用timer.cancel()