I'm trying to make a text-based game and want the text to print slowly. I created a method that does that, but when I use it for multiple strings, it doesn't separate the lines and just continues to print everything on the first line. I'm not sure exactly what I can do in this situation.
I'm really new to coding so if anyone knows a better way to make the slow printing text method on Java, please let me know.
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) {
}
}
}
}
You need to tell the computer to move to a new line. Perhaps the easiest way to change the code you have to do this is to add carriage return and line feed characters to the end of each line:
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) {
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.