我想解析一些競爭性編程問題的輸入測試用例,但程序只是坐着等待,即使我正在閱讀整行和「\ n」字符。Java:不能讓readline()工作
下面是代碼:
public static void main(String[] args) throws IOException {
String result = "", input = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while((input = br.readLine()) != null){
result += input;
}
System.out.print(result);
}
這是超級簡單,但它仍然無法正常工作。當我輸入(不含引號) 「1 2 3 4 5 6 7 8 9」然後按回車鍵,它只是等待更多的輸入。我怎樣才能使它工作,以便在我按下回車鍵後,程序離開while循環?
我可以爲多條線路做這項工作嗎?例如,在最後一個「\ n」之後,如果沒有更多字符,則跳出循環。
謝謝你的幫助。 :)
爲什麼需要一個while循環? – alfasin
這是2016年。你爲什麼不使用try-with-resources? – fge
我想輸入可變數量的行。 – Misha