-1

好的,我被要求分配一项制作反射游戏的任务,这将允许用户启动和停止计时器,然后在最后显示他们的时间。目前它主要工作,除了目前有一个问题,它会说按预期启动 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);
        }

    }

}
4

2 回答 2

0

移动

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

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

于 2013-11-14T16:59:30.680 回答
0

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

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

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

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

于 2013-11-14T17:16:16.560 回答