我的 do-while 語句遇到問題。我創建了一個 do-while 循環來確保唯一接受的輸入是“e”或“o”(不區分大小寫),但是,即使我插入了所需的輸入,它也會繼續循環。任何幫助表示贊賞!
1 回答

慕姐8265434
TA貢獻1813條經驗 獲得超2個贊
這句話:while(!side.equalsIgnoreCase("O") || !side.equalsIgnoreCase("E"));
永遠正確
如果您輸入E
or e
,則這!side.equalsIgnoreCase("E")
是 false,但這!side.equalsIgnoreCase("O")
是 true
如果您輸入O
or o
,則這!side.equalsIgnoreCase("O")
是 false,但這!side.equalsIgnoreCase("E")
是 true
由于您正在使用||
,true || false
給您true
所以循環永遠不會結束
對于每個其他輸入,兩者都為 true ( true || true
),這也是 true
您需要將其替換為:while(!side.equalsIgnoreCase("O") && !side.equalsIgnoreCase("E"));
添加回答
舉報
0/150
提交
取消