2011-01-19 135 views
6

如何让java更新当前控制台窗口而不是转到新行(或将新内容附加到旧的)。例如,如果我想演示进度,我会输出progress n,其中n将是给定的百分比。使用java更新控制台窗口

很显然,我想要做的就是用当前百分比更新n。任何帮助,将不胜感激。

回答

6

像这样的东西?

public class Test { 
    public static void main(String[] args) throws InterruptedException { 
     for (int i = 0; i < 100; i++) { 
      System.out.print("\rThinking... " + i); 
      System.out.flush(); 
      Thread.sleep(100); 
     } 
    } 
} 

它不能保证对课程的所有控制台(有些IDE可能很难)工作,但任何服从“回车”(即"\r")应该没问题。

+0

似乎无法正常工作,我只是用新的百分比对它们分别添加新行(在我的ide和命令提示符/ powershell中) – richzilla 2011-01-19 18:55:37