2016-10-07 5 views
1

Ich mache eine Codierung Übung auf codingbat und das ist, was ich tue ich nehme an:Boolesche Fehler auf codingbat

Gegeben 2 positive int Werte, geben Sie den größeren Wert, der im Bereich 10..20 inklusive , oder 0 zurückgeben, wenn keiner in diesem Bereich ist.

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

meinen Code:

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; 

} 

ich verstehe nicht, warum es nicht funktioniert, es mir diesen Fehler gibt:

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. 

Antwort

1

ich keine andere Erklärung hatte so die letzte Eingabe von a und b nicht hav Es hat funktioniert. Es sollte dies sein:

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