昨天我讀了關於for循環的Java逗號運算符。按我的預期工作。我想到了這種結構,但它沒有像預期的那樣工作。使用逗號運算符{(),{})是否有可能?
';' expected
} while((userInput < 1 || userInput > 3), wrongInput = true);
';' expected
} while((userInput < 1 || userInput > 3), wrongInput = true);
我的想法是,一個迭代後,如果userInput
不是1和3之間,它應該下一次迭代將顯示一條錯誤消息時設置布爾wrongInput
到true
讓。表示userInput
無效。
private int askUserToSelectDifficulty() {
int userInput;
Boolean wrongInput = false;
do{
if(wrongInput) println("\n\t Wrong input: possible selection 1, 2 or 3");
userInput = readInt();
} while((userInput < 1 || userInput > 3), wrongInput = true);
return userInput;
}
我想,也許是因爲這是在for循環的條件部分的等價內部,這是無效的語法。因爲你不能在條件部分使用逗號運算符?所使用的,我所看到的逗號操作
例子在for循環:Giving multiple conditions in for loop in JavaJava - comma operator outside for loop declaration
看看java運營商 - https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html – Joseph118 2014-11-25 11:56:02
和那些昏迷的例子被用來分配多個值,但沒有條件 – Joseph118 2014-11-25 11:57:08
@ Joseph118很高興知道NPE已經指出,它不是Java中的運營商。這確實不在文檔中。 – Joop 2014-11-25 12:06:08