【问题标题】:What's wrong with this conditional?这个条件有什么问题?
【发布时间】:2011-01-25 04:30:36
【问题描述】:

我正在尝试制作一种方法来测试 3 个长度是否可以组成一个三角形。我想我正在犯某种语法错误,但我不知道它是什么。

这是相关的代码:(它在java中)

public static void trya (int a, int b, int c)
{
    if (c>(a+b))
    {
        System.out.println ("yes") ;
    }
    else
    {
        if (b>(a+c)) 
        {
            System.out.println ("yes") ;
        }
    }
    else 
    { 
        if (a>(b+c))
        {
            System.out.println ("yes") ;
        }
    }
    else
    {
        System.out.println ("no") ;
    }

}

这是我得到的错误信息:

tryangle.java:17: 'else' without 'if'
        else 
                ^

【问题讨论】:

  • 错误信息告诉你什么?
  • 向我们提供您看到的任何错误,或说明为什么您认为您正在犯语法错误会很有用
  • 你得到什么错误?除此之外,一个改进是写else if (condition) {}而不是else { if (condition) {} }java.sun.com/docs/books/tutorial/java/nutsandbolts/if.html
  • 响应 "yes""no" 被交换。三元组[a=1 b=2 c=5]不能组成三角形。
  • @kenny,你的权利。我想知道为什么它不能正常工作。谢谢。

标签: java conditional


【解决方案1】:

第一个 if 有两个 else 块。尝试使用else if:

public static void trya (int a, int b, int c)
{
    if (c>(a+b))
    {
        System.out.println ("yes") ;
    }
    else if (b>(a+c)) 
    {
        System.out.println ("yes") ;
    }
    else if (a>(b+c))
    {
        System.out.println ("yes") ;
    }
    else
    {
        System.out.println ("no") ;
    }
}

【讨论】:

  • 其实有三个 else 块;)
【解决方案2】:

作为一名学生,我认为我将您指向 Java 在线文档的 Control Flow Statements 部分可能是合适的。

【讨论】:

    【解决方案3】:

    这是无效的:

    if (cond A) {
        // ...
    } else {
        if (cond B) {
            // ...
        }
    } else {
        if (cond C) {
            // ...
        }
    }
    

    应该是:

    if (cond A) {
        // ...
    } else if (cond B) {
        // ...
    } else if (cond C) {
        // ...
    }
    

    通过this Sun tutorial了解更多信息。

    【讨论】:

      【解决方案4】:

      就个人而言,我不太喜欢if/else

      public static boolean isValidTriangle(int a, int b, int c)
      {
          return (c > a + b) || (b > a + c) || (a > b + c);
      }
      
      public static void trya(int a, int b, int c)
      {
          System.out.println(isValidTriangle(a, b, c) ? "yes" : "no");
      }
      

      【讨论】:

        【解决方案5】:

        应该是:

        public static void trya (int a, int b, int c) 
        { 
            if (c>(a+b)) 
            { 
                System.out.println ("yes") ; 
            } 
            else if (b>(a+c))  
            { 
                System.out.println ("yes") ; 
            } 
            else  if (a>(b+c)) 
            { 
                System.out.println ("yes") ; 
            } 
            else 
            { 
                System.out.println ("no") ; 
            } 
        } 
        

        【讨论】:

          【解决方案6】:

          这是您的代码的格式:

          if (...) {...}
          else {...}
          else {...} //else than what?
          

          【讨论】:

          • 如果条件 (...) 为真,则执行 if 块。如果条件为假,则执行 else 块。你有一个条件和三个其他块。如果条件为假,将执行哪一个?
          【解决方案7】:

          还值得指出的是,您的方法实际上并没有测试三个长度是否可以组成一个三角形。例如,trya(1, 1, 4) 将导致打印 yes,即使边长 1、1、4 不形成三角形。

          【讨论】:

            【解决方案8】:

            同一 if 不能有两个 elses。更改嵌套,以便使用 else if 而不是

            else
            {
                if
            

            【讨论】:

              【解决方案9】:

              看起来问题是你有多个 else 块,一个 if 语句只能有一个 else 块。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2018-04-06
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-02-17
                • 2014-11-10
                相关资源
                最近更新 更多