如何在沒有其他版畫這一說法檢查值應該只有0或1,僅使用if塊
String val ="0";
if(val.equals("0") || val.equals("1"))
{
// Do nothing
}else{
System.our.println("Value should be either 0 or 1");
}
如何在沒有其他版畫這一說法檢查值應該只有0或1,僅使用if塊
String val ="0";
if(val.equals("0") || val.equals("1"))
{
// Do nothing
}else{
System.our.println("Value should be either 0 or 1");
}
String val ="0";
if(!val.equals("0") && !val.equals("1"))
{
System.our.println("Value should be either 0 or 1");
}
if(!(val.equals(「0」)|| val.equals(「1」))如果你懶惰,否定你已有的東西而不必去思考 – 2014-12-05 07:12:31
以下條件將這樣的伎倆:
if(!val.equals("0") && !val.equals("1"))
{
System.our.println("Value should be either 0 or 1");
}
的答案確實如下:
if(!val.equals("0") && !val.equals("1"))
{
System.our.println("Value should be either 0 or 1");
}
但是,你如何得到它呢?你想輸入你的陳述的else分支,所以你否定它。
val.equals()
否定變成!val.equals()
按道理OR
否定變得AND
(反之亦然),因此:||
成爲&&
如果你想使用||
運營商在你的問題中提到要實現它,然後,只需添加!
符號在開始,
String val ="0";
if(!(val.equals("0") || val.equals("1"))){
System.our.println("Value should be either 0 or 1");
}
檢查[德摩根法律](https://en.wikip edia.org/wiki/De_Morgan%27s_laws)。 – 2014-12-05 07:11:01
誰會upvote這??這個問題是非常微不足道的,問題隱約說明。 – Radiodef 2014-12-05 07:11:17
@Radiodef,它可能是,但突然間,我不能這個邏輯一點困惑,這是我張貼。現在我很清楚... – 2014-12-05 07:14:10