我在 Windows 上工作,我希望能够输出多行 超过 最后一个输出(也可能包含多行)。
我该怎么做呢?
例如:
会弹出一个简单的菜单,例如
- alphabet
- numbers
用户输入 1
输出:
一种
b
C
d
e
然后用户再次输入 2
我希望控制台删除最后的输出 block 并用新的输出替换它们。
所以
一种
b
C
d
e
将被替换为
1
2
3
4
在控制台中(它们的行数不同。)
最佳答案
试试这个:
import java.io.IOException;
import java.util.Scanner;
public class Test3{
public static void main(String args[]) throws IOException,InterruptedException{
Scanner sc = new Scanner(System.in);
int input = -2;
int prevInput = -2;
while(input != -1){
System.out.println("\nMenu");
System.out.println("---------");
System.out.println("1: Alphabet");
System.out.println("2: Numbers");
System.out.print("Enter input : ");
input = sc.nextInt();
if(input!=prevInput)
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
if(input == 1)
System.out.println("a\nb\nc\nd\ne\n");
else
System.out.println("1\n2\n3\n4");
prevInput = input;
}
}
}
关于java - 如何用另一个输出替换控制台中的最后一个输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61513901/