[英]JAVA - Why won't my while loop stop when I enter in filter or sort to leave it?
while((!(command.toLowerCase().contains("filter")==true) ||
!(command.toLowerCase().contains("sort")==true))){
System.out.println("Input is invalid. Please try again.");
command = range.nextLine();
}
你正在檢查使用||
你的一個陳述永遠是真的。 試試&&
。
您的情況可以改寫為:
(((command.toLowerCase().contains("filter")==false) ||
(command.toLowerCase().contains("sort")==false)))
所以它意味着,在人類語言中,雖然命令不包含文件管理器OR命令不包含排序,然后保持循環。 因此,如果只鍵入其中一個,則第二個條件為true,整個語句為true。 對於你所擁有的,如果你編寫包含它們的字符串,那么它就會結束,例如:“filter is sorted”。
如果你想只使用那個單詞中的一個結尾,你必須使用AND,這是&&,結果如下:
(((command.toLowerCase().contains("filter")==false) &&
(command.toLowerCase().contains("sort")==false)))
如果您的命令必須包含“過濾器”或“排序”。 然后,以下布爾表達式滿足該要求。
command.toLowerCase().contains("filter") || command.toLowerCase().contains("sort")
現在,如果我理解正確,你想在不滿足上述條件的同時繼續尋找命令。
while( !(command.toLowerCase().contains("filter") || command.toLowerCase().contains("sort")) )
上面的條件說“雖然NOT(過濾或排序)保持循環”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.