4

I am a new-comer to the java language, and I'm confused as to why I am getting an error here. It's a very short piece of code that I seem to have a mental block about. Any suggestions?

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;
        }
    }
}

Does anyone know what I'm missing here? (It's probably really obvious).

4

6 回答 6

14

Well, firstly, 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 this can be shortened as follows:

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

(make sure to upvote them for pointing those out :-)


However, your code is still wrong.

As stated here, Java's ^ operator is for exclusive bitwise OR, not exponentiation. Perhaps you want Math.pow()?

Returns the value of the first argument raised to the power of the second argument.

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

Or, you can also just use Math.hypot rather than rolling your own!

Returns sqrt(x^2 + y^2) without intermediate overflow or underflow.

public boolean checkCircleCollision(float x1, float y1, float r1,
    float x2, float y2, float r2) {
  return Math.hypot(x2 - x1, y2 - y1) <= (r1 + r2);
}
于 2012-08-23T02:05:24.143 回答
11

You can make the method's body much simpler...

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)    
    }
}

The result of <= is always a Boolean.

于 2012-08-23T02:04:06.710 回答
9

You forgot the else part of the if.

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;
    }
}

Which can be simplified to:

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

EDIT: Also, as noted by @veer, you are using ^2 when you should be using Math.pow, since the ^ operator in Java is a bitwise XOR, not the power operator. So go upvote and accept his answer, as that is the main cause of the error.

于 2012-08-23T02:03:24.890 回答
2

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;
    }
}

编译器会抛出异常:

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

为什么是这样?因为在方法结束时,编译器会找到一个无法返回值的路径,因此falseJava 编译器不会让程序崩溃或返回意外结果(比如没有关联的返回),而是会抛出异常。

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

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

  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 作为参数测试此方法,您将得到不同的结果:

虚空方法(1):

Welcome to theVoidMethod!

虚空方法(2):

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

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

于 2012-08-23T02:24:44.200 回答
2

试试这个,我已经使用 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));   
     } }
于 2012-08-23T02:09:41.043 回答
0

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

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
    }
}
}
于 2012-08-23T03:27:45.253 回答