2016-10-07 16 views
1

布爾的錯誤,我正在做的事情codingbat編碼的運動,這就是我該做的:上codingbat

鑑於2陽性int類型,返回較大值的範圍是10..20包容如果兩者都不在該範圍內,則返回0。

max1020(11,19)→19 max1020(19,11)→19 max1020(11,9)→11 max1020(9,21)→0

我的代碼:

public boolean IsInRange(int value) 
{ 
    return value >= 10 && value <= 20; 
} 

public int max1020(int a, int b) { 
    if (IsInRange(a) && IsInRange(b)) 
    return a > b ? a : b; 
    else if (IsInRange(a)) 
    return a; 
    else if (IsInRange(b)) 
    return b; 

} 

我不明白爲什麼它不工作,它給了我這個錯誤:

Error: public int max1020(int a, int b) { 
       ^^^^^^^^^^^^^^^^^^^^^ 
This method must return a result of type int 

Possible problem: the if-statement structure may theoretically 
allow a run to reach the end of the method without calling return. 
Consider adding a last line in the method return some_value; 
so a value is always returned. 

回答

1

我沒有一個else語句這樣的最後一個輸入和b不會甲肝工作。 應該是這樣的:

public boolean IsInRange(int value) { 
    return value >= 10 && value <= 20; 
} 

public int max1020(int a, int b) { 
    if (IsInRange(a) && IsInRange(b)) 
     return a > b ? a : b; 
    else if (IsInRange(a)) 
     return a; 
    else if (IsInRange(b)) 
     return b; 
    else 
     return 0; 
} 
相關問題