【问题标题】:Java Returning a Boolean ErrorJava 返回一个布尔错误
【发布时间】:2012-08-18 12:07:40
【问题描述】:

我是 java 语言的新手,我很困惑为什么我在这里遇到错误。 这是一段非常短的代码,我似乎有一个心理障碍。 有什么建议吗?

public class Rigidbody {
    public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){
        if(Math.sqrt(((x2-x1)^2)+((y2-y1)^2))<=(size1+size2)){
            return true;
        }
    }
}

有人知道我在这里缺少什么吗? (这可能真的很明显)。

【问题讨论】:

  • 只是为了将来参考,如果您发布实际错误而不是仅仅说“我遇到错误”总是更有帮助。看起来有几个人已经解决了这个问题,但他们可能会在错误消息的帮助下更快地解决问题。
  • 如果不满足if 条件,则您的代码不会返回布尔值,否则您需要返回其他部分。对于这些类型的编码,最好使用flags

标签: java methods boolean return collision


【解决方案1】:

首先,you forgot to have an else clause:

public boolean checkCircleCollision(float x1, float y1, float r1,
    float x2, float y2, float r2) {
  if (Math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)) <= (r1 + r2)){
    return true;
  } else {
    return false;
  }
}

Someone else already pointed out可以简写如下:

public boolean checkCircleCollision(float x1, float y1, float r1,
    float x2, float y2, float r2) {
  return Math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)) <= (r1 + r2);
}

(请务必为他们点赞 :-)


但是,您的代码仍然是错误的。

here 所述,Java 的^ 运算符用于按位异或,而不是求幂。也许你想要Math.pow()

返回第一个参数的第二个参数次方的值。

public boolean checkCircleCollision(float x1, float y1, float r1,
    float x2, float y2, float r2) {
  return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) <= (r1 + r2);
}

或者,您也可以只使用Math.hypot 而不是自己滚动!

返回 sqrt(x^2 + y^2) 没有中间溢出或下溢。

public boolean checkCircleCollision(float x1, float y1, float r1,
    float x2, float y2, float r2) {
  return Math.hypot(x2 - x1, y2 - y1) <= (r1 + r2);
}

【讨论】:

  • 好收获。 Math.pow() 是 OP 想要的 :)
  • 感谢您指出我的错误。也感谢您的快捷提示!
【解决方案2】:

您可以使方法的主体更简单...

public class Rigidbody {
    public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){
        return Math.sqrt(((x2-x1)^2)+((y2-y1)^2))<=(size1+size2)    
    }
}

&lt;= 的结果始终是布尔值。

【讨论】:

    【解决方案3】:

    您忘记了ifelse 部分。

    public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){
        if (Math.sqrt(Math.pow(x2-x1, 2)+ Math.pow(y2-y1,2)) <= (size1+size2)) {
            return true;
        } else {
            return false;
        }
    }
    

    可以简化为:

    public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){
      return Math.sqrt(Math.pow(x2-x1, 2)+ Math.pow(y2-y1,2)) <= (size1+size2);
    }
    

    编辑:另外,正如 @veer 所指出的,您在应该使用 Math.pow 时使用 ^2,因为 Java 中的 ^ 运算符是按位异或,而不是幂运算符。所以去upvote 并接受他的answer,因为这是错误的主要原因。

    【讨论】:

      【解决方案4】:

      由于^ 符号的使用,OP 在他/她的代码中存在问题,应将其替换为Math#pow(),每个人都好!但他/她在问缺少什么。答案很简单:每个方法都必须返回一个值,除了void 方法。

      如果你有

      public boolean aBooleanMethod(int x) {
          //some fancy and nice code!
          return true;
      }
      

      编译器会很高兴。但是,如果你这样做:

      public boolean anotherBooleanMethod(int x) {
          if (x < 2) {
              return true;
          }
      }
      

      编译器会抛出异常:

      此方法必须返回布尔类型的结果。

      这是为什么?因为在方法结束时,编译器会找到一个无法返回值的路径,所以 Java 编译器不会让你的程序崩溃或返回意外结果(如false,当没有关联的返回时),Java 编译器会抛出例外。

      如何解决?简单:不要忘记在方法中的每条路径上返回一个值

      最后一段代码解决问题的方法:

      1. 在方法的末尾添加一个默认的return。这是几乎所有程序员都使用的常用方法。

        public boolean anotherBooleanMethod(int x) {
            if (x < 2) {
                return true;
            }
            //if the method has never returned true, then it must return false...
            return false;
        }
        
      2. 对于布尔结果,您可以返回一个条件:

        public boolean anotherBooleanMethod(int x) {
            //your method always return a value, no compiler problems
            //the value depends if x is less than 2 (true) or 2 or more (false)
            return (x < 2);
        }
        

      关于void方法的例外,这并不意味着void方法不能使用return关键字,只是意味着它必须返回“nothing”。发布示例:

      public void theVoidMethod(int x) {
          System.out.println("Welcome to theVoidMethod!");
          if (x < 2) {
              //return nothing! End of the method
              return;
          }
          System.out.println("You've reached the bottom of theVoidMethod!");
          //here, the Java compiler will add a return sentence for you, no need to code it
          //return
      }
      

      如果你使用 1 和 2 作为参数来测试这个方法,你会得到不同的结果:

      theVoidMethod(1):

      Welcome to theVoidMethod!
      

      虚空方法(2):

      Welcome to theVoidMethod!
      You've reached the bottom of theVoidMethod!
      

      现在,要解决代码中的另一个问题,您应该使用 Math#pow 方法而不是 ^ 符号,但这在其他答案中有大量解释。

      【讨论】:

        【解决方案5】:

        试试这个,我用 Math.pow 作为正方形。还为 else 条件添加了 return 语句。

                public class Rigidbody {
        
             public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float                     y2,float size2){
                    return (Math.sqrt(Math.pow((x2-x1), 2) + Math.pow((y2-y1), 2)) <= (size1+size2));   
             } }
        

        【讨论】:

        • (P.S.别忘了给答案投票并将其标记为已解决。我需要它)
        【解决方案6】:

        当该条件为假时,它不会返回任何东西,这就是它错误的原因。

        public class Rigidbody {
        public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){
            if(Math.sqrt(((x2-x1)^2)+((y2-y1)^2))<=(size1+size2)){
                return true;
            }
            else{
                return false
            }
        }
        }
        

        【讨论】:

          猜你喜欢
          • 2017-07-31
          • 2014-05-16
          • 1970-01-01
          • 2013-01-04
          • 2015-05-03
          • 2013-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多