如何在Java中的for循环中实现计时器?

问题描述 投票:0回答:1

我最近开始学习Java,并尝试创建一个带有计时器功能的乘法游戏。

游戏的运行方式如下:

  • 玩家每轮参与五个问题
  • 必须在3秒内提供答案。
  • 每个正确答案可获得 20 分。

我一直致力于使用 Timer 和 TimerTask 实现计时器,并且它似乎部分有效。但是,我遇到了一个问题,即当时间到了时程序会卡住并且不会继续到循环的下一次迭代。

感谢您帮助回答我在 Stack Overflow 上的最初问题!

谢谢!

import java.util.Random;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class GameLogic {

    private static Timer timer;
    private static int score = 0;

    public static void playGame() {
        Scanner keyboard = new Scanner(System.in);

        // Generate five questions per a round
        int roundCount = 5;

        for (int i = 1; i <= roundCount; i++) {
            timer = new Timer();
            // Generate random numbers and multiply them
            Random random1 = new Random();
            Random random2 = new Random();
            int firstNum = random1.nextInt(9) + 1;
            int secondNum = random2.nextInt(9) + 1;
            int multiply = firstNum * secondNum;
            System.out.printf("%n%d X %d%n", firstNum, secondNum);


            // Schedule the timer task
            timer.schedule(new TimerTask() {
                public void run() {
                    System.out.println("Time's up");
                    timer.cancel();
                }

            }, 3000);


            int answer = keyboard.nextInt();

            if (keyboard.hasNextInt()) {
                answer = 0;
            } else {
                answer = keyboard.nextInt();
            }


            if (answer == multiply) {
                System.out.println("Correct!");
                score += 20;
            } else {
                System.out.println("Incorrect!");
            }
        }
    }
  
  public static void main(String[] args) {
    playGame();
  }
}
java loops timer timertask
1个回答
0
投票

我认为你的问题与计时器本身无关。您的代码的以下部分对我来说没有意义:

        int answer = keyboard.nextInt();

        if (keyboard.hasNextInt()) {
            answer = 0;
        } else {
            answer = keyboard.nextInt();
        }

用乘法打印问题后,代码读取一个整数。但随后代码会在 if 中等待下一个输入以检查其是否为整数,但不会读取它。您需要摆脱这个 if/else 块。

那么您应该在用户输入后取消计时器。在已经打印出时间已到之后取消计时器已经太晚了。如果时间到了,您可能需要退出游戏。

然后您可以在最后打印分数。最终的程序可能如下所示:

import java.util.Random;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

class GameLogic {

    private static Timer timer;
    private static int score = 0;

    public static void playGame() {
        Scanner keyboard = new Scanner(System.in);

        // Generate five questions per a round
        int roundCount = 5;

        for (int i = 1; i <= roundCount; i++) {
            timer = new Timer();
            // Generate random numbers and multiply them
            Random random1 = new Random();
            Random random2 = new Random();
            int firstNum = random1.nextInt(9) + 1;
            int secondNum = random2.nextInt(9) + 1;
            int multiply = firstNum * secondNum;
            System.out.printf("%n%d X %d%n", firstNum, secondNum);


            // Schedule the timer task
            timer.schedule(new TimerTask() {
                public void run() {
                    System.out.println("Time's up");
                    System.exit(1);
                }

            }, 3000);


            int answer = keyboard.nextInt();
            timer.cancel();

            if (answer == multiply) {
                System.out.println("Correct!");
                score += 20;
            } else {
                System.out.println("Incorrect!");
            }
        }
        System.out.println("Your score: " + score);
    }

    public static void main(String[] args) {
        playGame();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.