如何暂停我的 Java 程序 2 秒钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43507587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:57:22  来源:igfitidea点击:

How to pause my Java program for 2 seconds

javatimerpause

提问by Ethanph89

I'm new to Java and making a small game for practice.

我是 Java 新手,正在制作一个用于练习的小游戏。

if (doAllFaceUpCardsMatch == false) {
        //run pause here//
        concentration.flipAllCardsFaceDown();
} else {
        concentration.makeAllFaceUpCardsInvisible();
}

I want to pause the game for two seconds here before it does

我想在这里暂停游戏两秒钟之前

concentration.flipAllCardsFaceDown();

How would I go about pausing it?

我将如何暂停它?

采纳答案by Ashish Gupta

You can use:

您可以使用:

 Thread.sleep(2000);

or

或者

java.util.concurrent.TimeUnit.SECONDS.sleep(2);

Please note that both of these methods throw InterruptedException, which is a checked Exception, So you will have to catch that or declare in the method.

请注意这两个方法 throw InterruptedException,这是一个已检查的异常,因此您必须在方法中捕获或声明。

Edit: After Catching the exception, your code will look like this:

编辑:捕获异常后,您的代码将如下所示:

if (doAllFaceUpCardsMatch == false) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        concentration.flipAllCardsFaceDown();
} else {
        concentration.makeAllFaceUpCardsInvisible();
}

Since you are new, I would recommend learning how to do exception handling once you are little bit comfortable with java.

由于您是新手,我建议您在对 java 有点熟悉后学习如何进行异常处理。

回答by Lululicious

You can find a similar post here: How to delay in Java?

您可以在此处找到类似的帖子:How to delay in Java?

Basically what says in the old post. You could use java.util.concurrent.TimeUnit

基本上是旧帖子里说的。你可以使用 java.util.concurrent.TimeUnit

    import java.util.concurrent.TimeUnit;

    if (doAllFaceUpCardsMatch == false) {
        TimeUnit.SECONDS.sleep(2);
        concentration.flipAllCardsFaceDown();
    } else {
        concentration.makeAllFaceUpCardsInvisible();
    }

回答by ILIkeEggs

You can use Thread.currentThread().sleep(2000) to pause the current thread for 2 seconds (2000 milleseconds). You should surround this with a try/catch in case of InterruptedExceptions.

您可以使用 Thread.currentThread().sleep(2000) 将当前线程暂停 2 秒(2000 毫秒)。如果出现InterruptedException ,您应该用 try/catch 包围它。

回答by Scotty Stephens

For those just wanting a quick hack without having to bring in a library...

对于那些只想快速入门而不必带图书馆的人......

public class Timing {
    public static void main(String[] args) {
            int delay = 1000; // number of milliseconds to sleep

            long start = System.currentTimeMillis();
            while(start >= System.currentTimeMillis() - delay); // do nothing

            System.out.println("Time Slept: " + Long.toString(System.currentTimeMillis() - start));
    }
}

For high precision 60fps gaming this probably isn't what you want, but perhaps some could find it useful.

对于高精度 60fps 游戏,这可能不是您想要的,但也许有些人会发现它很有用。

回答by ACSJR

Another very simple way is creating the following method:

另一种非常简单的方法是创建以下方法:

public static void pause(long timeInMilliSeconds) {

    long timestamp = System.currentTimeMillis();


    do {

    } while (System.currentTimeMillis() < timestamp + timeInMilliSeconds);

}