该方法只能在windows下有效 idea下是无效的
package com.ym.contract;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static java.lang.Thread.sleep;
public class Test1 {
public static void main(String[] args) throws IOException, InterruptedException {
List<String> strings = new ArrayList<>() {{
add("d6");
add("d7");
add("d8");
add("d9");
add("d2");
add("d3");
add("d4");
add("d5");
}};
int i = 0;
while (i < 100) {
System.out.println("name = " + strings.get((int) (Math.random() * strings.size())));
// System.out.flush(); //无效
// System.out.print("\033[H\033[2J"); //无效
// Runtime.getRuntime().exec("cls"); //无效
sleep(100);
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); // 清屏命令
i++;
}
// System.out.println("(int) (Math.random() * 2 + 1) = " + (int) (Math.random() * strings.size()));
System.out.println("name = " + strings.get((int) (Math.random() * strings.size())));
}
}