samedi 27 décembre 2014

Covering all scenarios with if statement

I'm doing the below homework exercise


Given 2 int values greater than 0, return whichever value is nearest to 21 without going over. Return 0 if they both go over.


I've made the below code



public static void main(String[] args) {


System.out.println(blackjack(22,22));

System.out.println(blackjack(25,25));

System.out.println(blackjack(19,25));

System.out.println(blackjack(25,19));

System.out.println(blackjack(10,10));

System.out.println(blackjack(19,10));

System.out.println(blackjack(1,19));

}

// Create a method like:
public static int blackjack(int a, int b){
if ( a > 21 && b > 21){
return 0;
}
else if ( a <= 21 || b > 21){
return a;
}
else if ( a > 21 || b <= 21){
return b;
}
else if ( a >= b){
return a;
}
else {
return b;
}

}


All of it works except the last line of output in my main. I keep getting "a" or, "1" in this case, so I'm not sure what is wrong with my last line in my method declaration. I have a feeling something is wrong but I'm not sure what to change.


Aucun commentaire:

Enregistrer un commentaire