2015-10-21 206 views
0

这是我的方法,显示两个数字是否相等。最后一条if语句是,如果所有数字都是相同的,但是当我运行这个时,它会打印出“两并列第二”和“全部并列第一”。我如何做到这一点,如果所有的数字是相同的,它只会输出“所有并列第一”?如何解决我的方法来输出一个答案?

public static void overlap(double a, double b, double c){ 
    if (a==b) { 
     System.out.println("Two tied for second"); 
     } 
    if (c==b) { 
      System.out.println("Two tied for second"); 
     } 
    if (c==a) { 
      System.out.println("Two tied for second"); 
     } 
    if(a==b && b==c && a==c) { 
     System.out.println("All tied for first"); 
     } 
} 

回答

0

在进入其他条件语句之前,先将该语句放在开头。并确保使用ifif else陈述。

+1

您可能还想尝试使用else if语句。 – chasep255

+0

对不起,使用我的手机键入答案,所以没有看到完整的问题。再次阅读后更新它。 –

1

尝试使用else if

而且把最后一个条件,这否则将是真正的要早得多,第一:

if(a==b && b==c && a==c) { 
    System.out.println("All tied for first"); 
} 
else if (a==b) { 
    System.out.println("Two tied for second"); 
} 
else if (c==b) { 
    System.out.println("Two tied for second"); 
} 
else if (c==a) { 
    System.out.println("Two tied for second"); 
} 
+0

当你在这里时,考虑简化第一个表达式。并且考虑一下比较双打。 为什么a和b如果平等的话会并列第二?为什么不首先并列? –

0

像其他帖子说,如果也是一个不错的主意使用人,但要记住的是,当满足如果一个条件是另一个条件,它不会再进一步​​。所以如果它发现a == b,它不会再去检查c == b或c == a

怎么样?

public static void overlap(double a, double b, double c) { 
    if(a==b && b==c && a==c) { 
     System.out.println("a==b==c"); 
    } 
    else { 
     if (a==b) { 
      System.out.println("a==b"); 
     } 
     if (c==b) { 
      System.out.println("c==b"); 
     } 
     if (c==a) { 
      System.out.println("c==a"); 
     } 
    } 
} 
0

您有两个问题。

第一个是'if'语句的评估顺序。 这听起来像你想先评估最后一个。

第二个问题是您将要打印匹配的第一个“if”语句的结果,并跳过其余部分。

有很多方法可以做到这一点。 一种流行的方法是拥有if/else if语句链,但我发现它的可读性低于我喜欢的。所以我使用下面的,有些非标准的方法:

do { 
    if(a==b && b==c && a==c) { 
    System.out.println("All tied for first"); 
    break; 
    } 

    if (a==b) { 
    System.out.println("Two tied for second"); 
    break; 
    } 

    if (c==b) { 
    System.out.println("Two tied for second"); 
    break; 
    } 

    if (c==a) { 
    System.out.println("Two tied for second"); 
    break; 
    } 

} while(false); 
2

这将是更清洁。

public static void overlap(double a, double b, double c) { 

    if (a == b && b == c && a == c) { 
     System.out.println("All tied for first"); 
    } 
    else if (a == b || c == b || c == a) { 
     System.out.println("Two tied for second"); 
    } 
} 
+2

您只需要测试'a == b && b == c'。这是传递性财产。 –

0

您的代码的问题是它独立评估每个if语句。因此,如果您未能指定else语句,则每个if语句将在执行TRUE时执行,无论语句的顺序如何。

沿着相同的路线Moishe(也许更直观地为您的目的),你也可以组织你的代码像这样:

public static void overlap(double a, double b, double c){ 
    if(a==b && b==c && a==c) { 
     System.out.println("All tied for first"); 
     } 
    } 
    else { 
      if (a==b) { 
       System.out.println("Two tied for second"); 
      } 
      if (c==b) { 
       System.out.println("Two tied for second"); 
      } 
      if (c==a) { 
       System.out.println("Two tied for second"); 
      } 
    } 
} 

但是,如果你正在寻找精简事情有点,你可能会考虑重组事项以防止过度比较:

public static void overlap(double a, double b, double c){ 
    if (a==b) { 
     if(b==c && a==c) { 
      System.out.println("All tied for first"); 
      } 
     else { 
      System.out.println("Two tied for second"); 
      } 
     } 
    else if (c==b) { 
      System.out.println("Two tied for second"); 
     } 
    else if (c==a) { 
      System.out.println("Two tied for second"); 
     } 
} 
相关问题