2012-08-23 53 views
4

我是java語言的新來者,我很困惑,爲什麼我在這裏得到一個錯誤。 這是一段很短的代碼,我似乎有一個心理障礙。 有什麼建議嗎?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; 
     } 
    } 
} 

有沒有人知道我在這裏失蹤? (這可能很明顯)。

+3

僅供將來參考,如果您發佈實際錯誤而非僅僅說「我是一個錯誤」,它總是更有幫助。看起來好像有幾個人已經發現了這個問題,但是他們可能在錯誤信息的幫助下更快地完成了。 – DaoWen

+1

如果if條件不滿足,那麼你的代碼不會返回布爾值,否則你需要返回其他部分。對於這些類型的編碼,最好使用'flags'。 – exexzian

回答

14

嗯,首先,you forgot to have an else clause

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

(請務必給予好評他們指出那些出:-)


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這可以如下縮短

但是,您的代碼仍然是錯誤的。

如前所述here,Java的^運算符是針對專有按位OR,而非指數運算。也許你想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); 
} 
+1

好的。 'Math.pow()'是OP想要的:) – alex

+0

謝謝,指出我的錯誤。 也感謝您的快捷提示! –

9

您忘記了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注意到,您正在使用^2時,你應該使用Math.pow,因爲在^操作Java是一種按位異或,而不是電力運營商。所以去upvote並接受他的answer,因爲這是錯誤的主要原因。

11

可以使該方法的身體更簡單...

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

<=結果總是一個布爾值。

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

(P.S.不要忘記投票答案並將其標記爲已解決,我需要它) – AnujKu

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

編譯器將拋出一個異常:

此方法必須返回boolean類型的結果。

這是爲什麼?因爲在該方法結束時,編譯器會找到一個無法返回值的路徑,所以Java編譯器會拋出異常而不是使程序崩潰或返回意外的結果(如false,如果沒有關聯) 。

如何解決?簡單:不要忘記在您的方法的每個路徑上返回值

的方法來解決,在過去一段代碼的問題:

  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關鍵字,僅表示它必須返回「無」。要發佈一個樣本:

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! 

theVoidMethod(2):

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

現在,要解決代碼中的其他問題,應該使用Math#pow方法od而不是^符號,但這在其他答案中有很多解釋。

0

當條件爲false時,它不返回任何東西,這就是爲什麼它錯了。

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