![](/img/trans.png)
[英]Printing an Arraylist in toString with lines separating each entry
[英]Slow printing java text method not separating lines
我正在嘗試制作基於文本的游戲並希望文本打印緩慢。 我創建了一個這樣做的方法,但是當我將它用於多個字符串時,它不會分隔行,只是繼續在第一行打印所有內容。 我不確定在這種情況下我能做什么。
我對編碼真的很陌生,所以如果有人知道在 Java 上制作慢速打印文本方法的更好方法,請告訴我。
import java.util.concurrent.TimeUnit;
public class adventureGame {
public static void main(String[] args){
slowPrint("Text text text");
slowPrint("Text text text");
slowPrint("Text text text");
slowPrint("Text text text");
slowPrint("text text text");
slowPrint("Text text text");
}
public static void slowPrint(String output) {
for (int i = 0; i<output.length(); i++) {
char c = output.charAt(i);
System.out.print(c);
try {
TimeUnit.MILLISECONDS.sleep(100);
}
catch (Exception e) {
}
}
}
}
您需要告訴計算機換行。 也許更改代碼的最簡單方法是在每一行的末尾添加回車符和換行符:
import java.util.concurrent.TimeUnit;
public class adventureGame {
public static void main(String[] args){
slowPrint("Text text text\r\n");
slowPrint("Text text text\r\n");
slowPrint("Text text text\r\n");
slowPrint("Text text text\r\n");
slowPrint("text text text\r\n");
slowPrint("Text text text\r\n");
}
public static void slowPrint(String output) {
for (int i = 0; i<output.length(); i++) {
char c = output.charAt(i);
System.out.print(c);
try {
TimeUnit.MILLISECONDS.sleep(100);
}
catch (Exception e) {
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.