2016-10-17 36 views
0

如果我事先不知道行數,如何終止輸入循環?如果我事先不知道行數,如何終止輸入循環?

2015-08,2016-04

2015年8月15日,點擊數635
2016年3月24日,app_installs,683
2015年4月5日,收藏,763
2016- 01-22,收藏,788
2015年12月26日,點擊次數525
2016年6月3日,轉推,101
2015年12月2日,app_installs,982
2016年9月17日,app_installs, 770
2015-11-07,展示次數,245
2016年10月16日,展示567


我已經試過這
while (reader.hasNextLine())但期待另一個輸入。

+0

'if(shouldBreak)break;'不起作用? –

+3

你怎麼知道輸入完成的時間?如果您不知道,則無法對其進行編碼。 –

+0

你能分享你的代碼嗎?在沒有任何背景的情況下幫助你很難。 – Mureinik

回答

0

您可以使用破解關鍵字break在Java中的任何環,在while循環的情況:

while((reader.hasNextLine()) { 
    boolean shouldBreak = ... // your code when it should break 
    if (shouldBreak) break; 

} 
// here the execution will go after the loop 

break可以像/時/做任何的循環使用。

0
while(true) { //or any other condition 
    //do something 
    if (userInput.equals("&") { 
     break; 
    } 
} 

break關鍵字可以用於立即停止和逸出循環。它用於大多數編程語言。 還有一個有用的關鍵字會略微影響循環處理:continue。它立即跳轉到下一次迭代。

例子

int i = 0; 
while (true) { 
    if (i == 4) { 
     break; 
    } 
    System.out.println(i++); 
} 

會打印:

0 
1 
2 
3 

繼續:

int i = 0; 
while (true) { 
    if (i == 4) { 
     i++; 
     continue; 
    } 
    if (i == 6) { 
     break; 
    } 
    System.out.println(i++); 
} 

會打印:

0 
1 
2 
3 
5 
+0

輸入如上。最後一行之後沒有附加字符。 –

+0

@AzatDjanybekov從文件輸入?如果是,您將擁有EOF字符 – xenteros

+0

此輸入來自stdin。 –

相關問題