2012-01-08 43 views
0

有沒有辦法保留輸出而不打印,並最終在程序終止時打印出來?保持輸出不打印直到程序終止

例如

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
int t= Integer.parseInt(br.readLine()); 
for (int tc=0;tc<t;tc++) 
{ 
    String st = br.readLine(); 
    int len= st.length(); 
    System.out.println(len); //I don't want this to be printed on screen till the for loop terminates 
} 

的代碼的輸出將是

2 
abcd 
4 
aac 
3 

,而我要的是

2 
abcd 
aac 
4 
3 

是否有可能得到這個?

謝謝。

回答

4

串連要打印,循環

StringBuffer sb = new StringBuffer(); 
for (int tc=0;tc<t;tc++) 
{ 
    // ... 
    //System.out.println(len); 
    sb.append(len).append("\\n"); 
} 

System.out.println(sb.toString()); 
+0

謝謝後打印出來的!那很整齊。我不得不放置一個反斜槓而不是2.也就是說,sb.append(len).append(「\ n」); – Bharat 2012-01-08 18:32:22