0
我有麻煩在底部我試圖設置退出哨兵退出鍵.. 即時通訊不知道我到底應該如何做到這一點。哨兵問題與循環,以允許進入退出結束程序在java
int number;
do
{
Scanner Input = new Scanner(System.in);
System.out.print("Enter a positive integer or q to quit program:");
number = Input.nextInt();
}
while(number >= 0);
do
{
Scanner Input = new Scanner(System.in);
System.out.print("Input should be positve:");
number = Input.nextInt();
}
while(number < 0);
do
{
Scanner quit = new Scanner(System.in);
System.out.print("Enter a positive integer or quit to end program");
input = quit.nextstring();
}
while (!input.equals(quit))//sentinel value allows user to end program
{
quit = reader.next()
是它我在找什麼,但我需要結束循環爲q而不是0 .. does布爾接受這兩種類型? – 2014-09-20 21:18:43
'boolean'類型表示真或假,而不是其他類型。你想檢查'input.nextLine()'用戶輸入,它不存儲在'endLoop',這是你的哨兵變量。我會更新我的答案,以顯示如何檢查'q'。 – legendof 2014-09-22 17:26:10
我建議刷新Java類型:http://docs.oracle.com/javase/tutorial/java/generics/types.html – legendof 2014-09-22 17:33:49