Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在文本显示之间暂停_Java_Sleep_Pause - Fatal编程技术网

Java 在文本显示之间暂停

Java 在文本显示之间暂停,java,sleep,pause,Java,Sleep,Pause,所以我正在创建一个基于文本的游戏,我很难找到在文本显示之间暂停的方法。。我知道线程睡眠会起作用,但我不想对数百行文本这样做。这就是我所拥有的,但是有没有更简单的方法呢 public class MyClass { public static void main(String[] args) { System.out.println("Hello?"); //pause here System.out.println("Is this thi

所以我正在创建一个基于文本的游戏,我很难找到在文本显示之间暂停的方法。。我知道线程睡眠会起作用,但我不想对数百行文本这样做。这就是我所拥有的,但是有没有更简单的方法呢

public class MyClass {
    public static void main(String[] args) {
        System.out.println("Hello?");
        //pause here
        System.out.println("Is this thing on?");
        /**Obviously I have a pause here, but I dont want to have to use this every time.*/
        try {
            Thread.sleep(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Now, what is your name?");
    }
}
退房

您可以这样使用它:

public static void main(String[] args) {
    Timer timer = new Timer();
    writeIn(timer, 3000, "Hello?");
    writeIn(timer, 6000, "Is this thing on?");
    writeIn(timer, 12000, "Now, what is your name?");
    closeTimerIn(timer, 13000); // clean exit
}

private static void writeIn(Timer timer, final int millis, final String text){
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            System.out.println(text);
        }
    };
    timer.schedule(task, millis);
}

private static void closeTimerIn(final Timer timer, final int millis){
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            timer.cancel();
        }
    };
    timer.schedule(task, millis);
}

正如m.antkowicz在评论中提到的,您需要创建一个print方法。我举了一个例子:

void printAndSleep(String string, int sleepTimeMs) {
  System.out.println(string);
  Thread.sleep(sleepTimeMs);
}
现在,任何时候你想要打印,只需使用上面我们制作的printAndSleep方法。像这样:

printAndSleep("Hello?", 1000); // Print and pause for 1 second
printAndSleep("Is this thing on?", 2000); // Print and pause for 2 seconds

为什么不把它简单地包装在另一个类中呢

class DisplayWrapper {
    private final static long DEFAULT_PAUSE_MS = 250;

    public static void outputWithPause(String text, long ms) {
        System.out.println(text);
        try {
            Thread.sleep(ms);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void show(String text) {
        outputWithPause(text. DEFAULT_PAUSE_MS);
    }
}

public class MyClass {
public static void main(String[] args) {
    DisplayWrapper.show("Hello?");
    DisplayWrapper.show("Is this thing on?");
    DisplayWrapper.show("Now, what is your name?");
}

要重构代码,您只需创建自己的print()方法,该方法将在打印文本后暂停编写一个方法,称之为
pause
,可能允许调用者传入
int
值,指示他们要暂停的时间。我不认为为这一功能创建整个类是一个好主意或好设计。类应该包含比单个函数更多的功能。这应该写在函数本身中