我试图创建一个基本的非静态文本行,但不会重新打印,但\r
要么无法正常工作,要么我做错了;我是java的新手,所以如果答案非常明显,我很抱歉。这是我的代码:
import java.util.Scanner;
public class TextFight {
public static void main(String args[]) {
int MonsterHealth = 10;
int YourHealth = 15;
while(true) {
System.out.println("............");
System.out.print("\r............");
System.out.println("| |");
System.out.print("\r| |");
System.out.println("| 0 0 |");
System.out.print("\r| 0 0 |");
System.out.println("| ------- |");
System.out.print("\r| ------- |");
System.out.println("............");
System.out.print("\r............");
System.out.println(MonsterHealth);
System.out.print("\r" + MonsterHealth);
System.out.println("");
System.out.println("");
System.out.println("............");
System.out.print("\r............");
System.out.println("| |");
System.out.print("\r| |");
System.out.println("| 0 0 |");
System.out.print("\r| 0 0 |");
System.out.println("| ------- |");
System.out.print("\r| ------- |");
System.out.println("............");
System.out.print("\r............");
System.out.println("");
System.out.println(YourHealth);
System.out.print("\r" + YourHealth);
}
}
}
它正在做的是:
15............
............| |
| || 0 0 |
| 0 0 || ------- |
| ------- |............
............10
10
............
............| |
| || 0 0 |
| 0 0 || ------- |
| ------- |............
............
15
15............
............| |
| || 0 0 |
| 0 0 || ------- |
| ------- |............
............10
请再次给我代码示例我是初学者。
答案 0 :(得分:1)
如果您希望每个部分都在新行上,则应使用println
代替print
:
System.out.println("............");
System.out.println("............");
System.out.println("| |");
System.out.println("| |");
System.out.println("| 0 0 |");
...
或添加换行符"\n"
并使用print
:
System.out.print("\n............");
System.out.print("\n............");
...