0

Compare three boolean values and display the first one that is true.

Hey guys, I am trying to write a program that compares three boolean values and displays the first true one. I am comparing three words for their length, and it will display the longest. The error that I am getting is that my else tags aren't working. Take a look at the code.

//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);

I have set boolean values for word1bt2, word2bt3 and word1bt3. In eclipse, I am getting a syntax error under the elses in my code above. Any help would be great!

4

5 回答 5

3
if (word1bt2 == true && word1bt3 == true);

错了,需要去掉分号:

if (word1bt2 == true && word1bt3 == true)

elses也一样


else (word2bt3 == true);

也错了,应该是

else if (word2bt3 == true)

旁注:布尔值可以用作条件,因此您的if语句应该是

if (word1bt2 && word1bt3) // The same as if (word1bt2 == true && word1bt3 == true)
于 2013-09-09T12:49:13.803 回答
2

如何比较三个布尔值?

不!

如果您发现自己需要比较三个变量,您也可以立即满足任意数量的变量 - 没有必要闲逛 - 立即正确进行。

public String longest(Iterator<String> i) {
  // Walk the iterator.
  String longest = i.hasNext() ? i.next() : null;
  while (i.hasNext()) {
    String next = i.next();
    if (next.length() > longest.length()) {
      longest = next;
    }
  }
  return longest;
}

public String longest(Iterable<String> i) {
  // Walk the iterator.
  return longest(i.iterator());
}

public String longest(String... ss) {
  // An array is iterable.
  return longest(ss);
}
于 2013-09-09T13:02:47.820 回答
1

删除;并用括号更改它{}

if (word1bt2 && word1bt3) {
      System.out.println(wor1);
} else if (word2bt3) {
      System.out.println(wor2);
} else {
      System.out.println(wor3);
}
于 2013-09-09T12:49:11.257 回答
0

else 块的问题:使用{}insteaad of()来附上指令......

删除 ;第一个如果!!!!!!- 很常见的错误,结果非常令人费解!

//Check which word is the longest
if (word1bt2 == true && word1bt3 == true) { //leave ; and always add bracket!
    System.out.println(wor1);
}
else if(word2bt3 == true)
{
    System.out.println(wor2);
}
else {
    System.out.println(wor3);
}  
  • 如果您在 else 分支中需要条件,则必须再次使用 if - 普通 else 不会有这样的功能......
  • 总是为 if 语句、循环等的主体使用括号!!!
  • 小心不要使用 ; 在表现不佳的行中:
    • if 语句
    • for 循环
    • while() {...} 循环的 while 语句
于 2013-09-09T12:50:58.770 回答
0

试试这个,如果长度相等,那么 s1 被认为是更大的。我也没有添加空检查

 public class Test {
            public static void main(String[] args) {

            String word1 = "hi";
            String word2 = "Hello";
            String word3 = "Hell";
            String Bigger = null;
            if(word1.length() >= word2.length() && word1.length() >= word3.length() ){
                Bigger = word1;
            }else if(word2.length() >= word1.length() && word2.length() >= word3.length()){
                Bigger = word2;
            }else if(word3.length() >= word2.length() && word3.length() >= word1.length()){
                Bigger = word3;
            }
            System.out.println(Bigger);

        }

    }
于 2013-09-09T12:58:44.767 回答