【发布时间】:2016-06-29 18:10:20
【问题描述】:
class progReport extends Thread {
// function for clearing the line by printing escape char '\b'
private static void cls(){
for (int i = 0; i <50; i++) {
System.out.print("\b");
}
} //End of cls
//Running a thread for printing the message every 100 ms.
public void run(int prog) {
int k;
cls();
try {
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print("Scanning: " + (prog) + "% ");
k=prog%3; // mod 3, so as to decide which symbol to be used for revolving animation
switch (k) {
case 0:
System.out.print("/");
break;
case 1:
System.out.print("--"); // LINE WITH THE PROBLEM
break;
default:
System.out.print("\\");
break;
} // End of switch
} // End of run
} // end of class progReport
// class with main
public class ProgressDemo {
public static void main(String...a) {
int prog = 0;
progReport pr = new progReport();
while(prog <= 100)
pr.run(prog++);
System.out.println("\nScanning Complete!!");
} // End of main
} // End of class ProgressDemo
在命令行上执行此代码时,它不会清除案例 1 的最后一个“-”:其中两个破折号打印“--”,但如果我在其他情况下包含一个空格(即将“/”和“\\”更改为“/”和“\\”)然后它正在正确清除整行。有人可以帮我指出为什么它的行为如此奇怪吗?
【问题讨论】:
标签: java multithreading command-prompt unicode-escapes