Last active
October 14, 2017 23:27
-
-
Save jsbonso/f4f7e5b89ea749825658b7d0d6bb47cb to your computer and use it in GitHub Desktop.
Java Blackjack Checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Simple blackjack method that: | |
* | |
* 1. Returns the input which is the nearest to number 21 | |
* 2. Returns 0 if both numbers are over 21 | |
* @author Jon Bonso | |
* @param a | |
* @param b | |
*/ | |
static int blackjack(int a, int b) { | |
if (a > 21 && b > 21) { | |
return 0; | |
} | |
// these ternary operators simply read as | |
// "If the number is over 21, then return 0, else, retain its value" | |
// so we only return the input with the greater value | |
// than the other input, that does not go over 21. | |
return ( ((a > 21) ? 0 : a) > | |
((b > 21) ? 0 : b)) | |
? a : b ; | |
} | |
public static void main(String... args) { | |
System.out.println( blackjack(19, 21) ); | |
System.out.println( blackjack(21, 19) ); | |
System.out.println( blackjack(19, 22) ); | |
System.out.println( blackjack(23, 18) ); | |
System.out.println( blackjack(23, 24) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment