Java 调用时的额外输出

Java 调用时的额外输出,java,timer,duplicate-removal,Java,Timer,Duplicate Removal,好的,我被要求分配一个任务来制作一个反射游戏,这将允许用户启动和停止计时器,然后在最后显示他们的时间。目前它主要在工作,但目前存在一个问题,即它会说按1键按预期启动并启动我的计时器,但随后它会在显示非预期秒数之前再次显示此消息。因此,如果有人能提供帮助或指出可能导致这种情况的原因,我们将不胜感激 public class TimerTest { static int attempts = 4; static Timer timer = new Timer(); stat

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

    }

}
移动


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

假设您希望迭代while循环(并显示消息)的次数与尝试次数相同:

将语句置于一个条件下,仅在控制器不是1时打印

if (controler != 1) {
    System.out.println("\t\t\tplease push 1 to start");
}
controler = keyboard.nextInt();
另外:请注意,您需要以某种方式停止计时器。您可能希望保留对它的引用,并在按下“2”时调用
timer.cancel()

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