所以我正在创建一个基于文本的游戏,并且我很难找到一种在文本显示之间暂停的方法。我知道线程 sleep 会起作用,但我不想这样做对数百行文本执行此操作。这是我所拥有的,但是有更简单的方法吗?
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?");
}
}
最佳答案
为什么不简单地将它包装在另一个类中?
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?");
}
关于java - 文本显示之间暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35002002/