2017-02-19 48 views
0
while(count <= maxNum){ 
    System.out.println("O-------O"); 
    System.out.printf("|" + "%8s", count + "|"); 
    System.out.println("O-------O"); 
    count++; 
    startTime = System.currentTimeMillis(); 
    //an empty loop! does nothing except keeps checking the continuation condition 
    //continues as long as the time that has passed is less than millisToWait 
    while(System.currentTimeMillis() - startTime < millisToWait); 
    System.out.println("O-------O"); 

上面是代碼的一部分,它展示了我試圖實現的實際輸出。代碼大部分按預期工作,除了輸出(下面是作爲圖像附加的輸出)不是100%正確的。每個數字右邊都有一個額外的「O ------- O」。Java字符串格式問題

Here's the output

糾正我,如果我錯了,但我相信它是與第一個println語句,但我仍然不知道它是如何一次打印在上面,然後一次側。任何幫助表示讚賞。

回答

1

printf不會自動添加換行符,您必須手動添加換行符到字符串中。出於便攜性的原因,我們使用\ n的%n instaid。格式也可以簡化。

更改第三行:

System.out.printf("|%8s%n|", count); 
+0

哇。這一修改固定它。非常感謝。 –

+0

@shmosel謝謝,我將它添加到答案中。 – Henrykvdb