我正在爲介紹CS的一個學校項目工作,我不能發佈我的代碼,因爲我們的老師讓我們從StackOverflow檢查代碼失敗。我的問題是,在我的代碼的格式:雖然循環不等待如果語句的完成
while (condition){
Do something;
if (new condition){
Do something else;
Wait for input;
Depending on input make while's condition false;
}
此代碼應等待輸入時if語句進行評估,它別的東西。但是,我的代碼不會等待「等待輸入」步驟並直接進入「執行某些」步驟。這是一些代碼。感謝您的幫助。
while (inum != 102) {
System.out.println("Enter a number between 1 and 100: ");
inum = input.nextInt();
else if (inum == 101) {
System.out.println("Are you sure you want to quit?");
confirm = input.nextLine();
if (confirm == "yes") {
inum = 102;
}
}
下面的代碼給了我這個當我在101型: 你確定要退出? 輸入1和100之間的數字:
*代碼不等待
confirm = input.nextLine();
if (confirm == "yes") {
inum = 102;
}
步驟。
@SotiriosDelimanolis等 - 如果代碼 – nachokk
中未顯示任何先前的「if」最常問到的問題在SO上,它似乎...如果(確認==「是」)'用'if(「yes」.equals(confirm))替換' – fge
我改變後仍然得到相同的響應(「yes」.equals (確認)) – user3389265