Java 如何比较三个布尔值

Java 如何比较三个布尔值,java,boolean,syntax-error,Java,Boolean,Syntax Error,比较三个布尔值并显示第一个为真的值 嘿,伙计们,我正试图编写一个程序,比较三个布尔值并显示第一个真值。我正在比较三个单词的长度,它将显示最长的。我得到的错误是我的else标记不起作用。看看代码 //Check which word is bigger if (len1 > len2) word1bt2 = true; if (len2 > len3) word2bt3 = true; if (len1 > len3)

比较三个布尔值并显示第一个为真的值

嘿,伙计们,我正试图编写一个程序,比较三个布尔值并显示第一个真值。我正在比较三个单词的长度,它将显示最长的。我得到的错误是我的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设置了布尔值。在eclipse中,我在上面代码的ELSE下遇到了语法错误。任何帮助都会很好

删除
并用括号将其更改为
{}

if (word1bt2 && word1bt3) {
      System.out.println(wor1);
} else if (word2bt3) {
      System.out.println(wor2);
} else {
      System.out.println(wor3);
}

删除
并用括号将其更改为
{}

if (word1bt2 && word1bt3) {
      System.out.println(wor1);
} else if (word2bt3) {
      System.out.println(wor2);
} else {
      System.out.println(wor3);
}
如果错误,则需要删除分号:

if (word1bt2 == true && word1bt3 == true)
对于
else
s,相同


这也是错误的,应该是

else if (word2bt3 == true)
if (word1bt2 && word1bt3) // The same as if (word1bt2 == true && word1bt3 == true)

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

else if (word2bt3 == true)
if (word1bt2 && word1bt3) // The same as if (word1bt2 == true && word1bt3 == true)
如果错误,则需要删除分号:

if (word1bt2 == true && word1bt3 == true)
对于
else
s,相同


这也是错误的,应该是

else if (word2bt3 == true)
if (word1bt2 && word1bt3) // The same as if (word1bt2 == true && word1bt3 == true)

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

else if (word2bt3 == true)
if (word1bt2 && word1bt3) // The same as if (word1bt2 == true && word1bt3 == true)

else块的问题:使用
{}
代替
()
来封装指令

删除
在第一个if相当常见的错误,结果令人费解

//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-plain-else不会有这样的功能
  • 始终对if语句、循环等的主体使用括号
  • 使用时要格外小心不要;在与之不符的行中:
    • 如果语句
    • for循环
    • while(){…}循环while语句

else块的问题:使用
{}
代替
()
来封装指令

删除
在第一个if相当常见的错误,结果令人费解

//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-plain-else不会有这样的功能
  • 始终对if语句、循环等的主体使用括号
  • 使用时要格外小心不要;在与之不符的行中:
    • 如果语句
    • for循环
    • while(){…}循环while语句

试试这个,如果长度相等,则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);

        }

    }

试试这个,如果长度相等,那么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);

        }

    }
如何比较三个布尔值

不要

如果你发现自己需要比较三个变量,你也可以立即考虑任意数量的变量——没有必要闲逛——马上正确地做

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);
}
公共字符串最长(迭代器i){
//遍历迭代器。
字符串最长=i.hasNext()?i.next():null;
while(i.hasNext()){
字符串next=i.next();
if(next.length()>longest.length()){
最长=下一个;
}
}
返回时间最长;
}
公共字符串最长(Iterable i){
//遍历迭代器。
返回最长的(i.迭代器());
}
公共字符串最长(字符串…ss){
//数组是可编辑的。
返回时间最长(ss);
}
如何比较三个布尔值

不要

如果你发现自己需要比较三个变量,你也可以立即考虑任意数量的变量——没有必要闲逛——马上正确地做

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);
}
公共字符串最长(迭代器i){
//遍历迭代器。
字符串最长=i.hasNext()?i.next():null;
while(i.hasNext()){
字符串next=i.next();
if(next.length()>longest.length()){
最长=下一个;
}
}
返回时间最长;
}
公共字符串最长(Iterable i){
//遍历迭代器。
返回最长的(i.迭代器());
}
公共字符串最长(字符串…ss){
//数组是可编辑的。
返回时间最长(ss);
}


else标签
什么是else标签?你需要在第一个else if(cond){…code…}后面加if,else if(cond2){…code…}else{…code…}你还需要修正分号。在这里你应该找到好的答案:如果(xyz==true),请不要写
。只要写
if(xyz)
。将布尔值与
true
进行比较是没有意义的。你不会写
if((len1>len2)==true)
,你会写吗?
else标签
什么是else标签?你需要在第一个else标签后加if if(cond){…code…}else if(cond2){…code…}else{…code…}你还需要修正分号。在这里你应该找到好的答案:如果(xyz==true),请不要写
。只要写
if(xyz)
。将布尔值与
true
进行比较是没有意义的。如果((len1>len2)==true)
,你就不会写
,对吗?在这种情况下,括号是不需要的,但在这种情况下,代码更容易阅读。此外,
==true
部分是多余的。如果你删除所有过时的
==true
片段,阅读起来就更容易了。在初学者的问题中看到它们是一回事,但在答案中重复它们又是另一回事。在特定情况下不需要括号是对的,但在这种情况下代码更容易阅读。此外,
==true
部分是多余的。如果删除所有过时的
==true
片段,阅读起来更容易。在初学者的问题中看到它们是一回事,但在答案中重复它们又是另一回事。是的,我在帖子中看到了
else if
我自己。没有复制粘贴。谢谢,我是Java新手,所以这个网站对我来说很棒。
if(bool1&&bool2)
应该使用。诸如
bool==true
之类的表达式是皱眉的upon@rocketboy完全忘记了,我更新了答案,谢谢!赞成