2017-07-17 108 views
1

我編寫了一個小小的java程序。 每一秒我在控制檯如何在Java中創建CommandLine(控制檯)

System.out.println("Test"); 

我在當下寫命令打印的值,如果System.out中。循環開始,我的命令就會消失,並與System.out.println(「」)一起上升。

這是我的代碼:

public class Main{ 
public static String s; 
public static void main(String[] args) { 

    printLogo(); 

    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new TimerTask() { 
     public void run() { 

      Data.cpuUsage = ((OperatingSystemMXBean) ManagementFactory 
               .getOperatingSystemMXBean()) 
               .getSystemCpuLoad() * 100; 
      Data.totalMem = ((OperatingSystemMXBean) ManagementFactory 
               .getOperatingSystemMXBean()) 
               .getFreePhysicalMemorySize()/1000000; 

      System.out.println(Data.totalMemory); 
     } 
    }, 1000, 1000); 

    System.out.println("Please type a command:"); 

也許有人可以幫我嗎? 對不起,我的英語不好。

+0

您的主要方法正在終止,因此定時器正在被終止。看看使用'ScheduledThreadExecutor'。 –

+0

你知道一個網站或某事嗎?其他? – Pascal

+0

在這裏檢查:https://stackoverflow.com/questions/1321620/waiting-for-a-timer-to-finish-in-java –

回答

0

你的問題是你每秒都在打印。每當使用類型的東西,它將被連接到最後一行打印。如果你想在終端中使用某種形式的UI,你將不得不使用某種類型的庫(或者至少不只是System.out。*函數)。

結賬Charva