当前实例上的 java 计时器

发布于 2024-08-27 02:40:02 字数 1477 浏览 3 评论 0原文

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


public class Boggle {
    Board board;
    Player player;
    Timer timer;
    boolean active;

    static Scanner in = new Scanner(System.in);

    public Boggle() {
        board = new Board(4);
        timer = new Timer();
    }

    public void newGame() {
        System.out.println("Please enter your name: ");
        String line = in.nextLine(); 
        player = new Player(line);
        active = true;

        board.shuffle();
        System.out.println(board);

        timer.schedule(new timesUP(), 20000);
        while(active) {
            String temp = in.nextLine();
            player.addGuess(temp);
        }
    }


    public void endGame() {
        active = false;
        int score = Scoring.calculate(player, board);
        System.out.println(score);
    }


    class timesUP extends TimerTask {
        public void run() {
            endGame();
        }
    }


    public static void main(String[] args) {
            Boggle boggle = new Boggle();
            boggle.newGame();

    }
}

我有上面的类,它应该在给定的时间长度内执行循环,然后调用实例方法。本质上,我需要 newGame() 中的循环在当前实例上调用 endGame() 之前运行一分钟左右。但是,使用 Timer 类,我不确定如何调用当前实例所需的方法,因为我无法将任何参数传递给 timertasks run 方法?

有没有一种简单的方法可以做到这一点,或者我是否以错误的方式进行此操作? (注意:这只是一个控制台项目,没有 GUI)

==========

代码已编辑

我已按照建议将代码更改为上面的代码,它几乎按照我的预期工作,但线程仍然没有似乎结束得很好。我认为 while 循环会死掉,控制权最终会回到 main 方法。有什么想法吗?

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


public class Boggle {
    Board board;
    Player player;
    Timer timer;
    boolean active;

    static Scanner in = new Scanner(System.in);

    public Boggle() {
        board = new Board(4);
        timer = new Timer();
    }

    public void newGame() {
        System.out.println("Please enter your name: ");
        String line = in.nextLine(); 
        player = new Player(line);
        active = true;

        board.shuffle();
        System.out.println(board);

        timer.schedule(new timesUP(), 20000);
        while(active) {
            String temp = in.nextLine();
            player.addGuess(temp);
        }
    }


    public void endGame() {
        active = false;
        int score = Scoring.calculate(player, board);
        System.out.println(score);
    }


    class timesUP extends TimerTask {
        public void run() {
            endGame();
        }
    }


    public static void main(String[] args) {
            Boggle boggle = new Boggle();
            boggle.newGame();

    }
}

I have the above class which should perform a loop for a given length of time and afterwards invoke an instance method. Essentially I need the loop in newGame() to run for a minute or so before endGame() is invoked on the current instance. However, using the Timer class I'm not sure how I would invoke the method I need on the current instance since I can't pass any parameters to the timertasks run method?

Is there an easy way to do this or am I going about this the wrong way? (note: this is a console project only, no GUI)

==========

code edited

I've changed the code to the above following the recommendations, and it works almost as I expect however the thread still doesnt seem to end properly. I was the while loop would die and control would eventually come back to the main method. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风为裳 2024-09-03 02:40:02

因为 timesUP (请更改名称!)是一个内部类,所以它已经拥有对创建它的 Boggle 实例的引用。 (如果将其标记为static,情况就不会如此。)如果您想创建与另一个实例关联的 timesUP 实例,您可以执行类似的操作:

otherBoggle.new timesUp();

诚然,这是相当奇怪的语法 :)

诚然,这并不能解决 Midhat 发现的问题 - 但这意味着您无需担心获取对外部类的引用。只需从 timesUP.run() 中调用 endGame() ,它就会在适当的实例上调用它。

Because timesUP (please change the name!) is an inner class, it already has a reference to the instance of Boggle which created it. (That wouldn't be the case if it were marked as static.) If you wanted to create an instance of timesUP associated with another instance, you'd do something like:

otherBoggle.new timesUp();

It's pretty odd syntax, admittedly :)

This doesn't fix the problem that Midhat identified, admittedly - but it means you don't need to worry about getting a reference to the outer class. Just call endGame() from within timesUP.run() and it will call it on the appropriate instance.

树深时见影 2024-09-03 02:40:02

您在线程中阻塞了控制台输入。运行时间将由此决定。您可以将控制台输入放在单独的线程中,并在当前线程中添加一个计时器,在一分钟后终止输入线程,然后调用 this.endGame()

You have blocking console input in the thread. The running time will be dictated by that. You can have the console input in a seperate thread, and add a timer in the current thread to kill the input thread after a minute, and call this.endGame()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文