比较三个 boolean 值并显示第一个为 true 的值。
嘿伙计们,我正在尝试编写一个程序来比较三个 boolean 值并显示第一个真值。我正在比较三个单词的长度,它会显示最长的。我收到的错误是我的 else 标签不起作用。看一下代码。
//Check which word is bigger
if (len1 > len2)
word1bt2 = true;
if (len2 > len3)
word2bt3 = true;
if (len1 > len3)
word1bt3 = true;
//Check which word is the longest
if (word1bt2 == true && word1bt3 == true);
System.out.println(wor1);
else if (word2bt3 == true);
System.out.println(wor2);
else System.out.println(wor3);
我已经为 word1bt2、word2bt3 和 word1bt3 设置了 boolean 值。在 Eclipse 中,我在上面的代码中的 else 下遇到语法错误。任何帮助都会很棒!
最佳答案
if (word1bt2 == true && word1bt3 == true);
错误,需要去掉分号:
if (word1bt2 == true && word1bt3 == true)
其他也一样
else (word2bt3 == true);
也错了,应该是
else if (word2bt3 == true)
<小时/>
旁注: boolean 值可以用作条件,因此您的 if
语句应该是
if (word1bt2 && word1bt3) // The same as if (word1bt2 == true && word1bt3 == true)
关于java - 如何比较三个 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18698586/