Skip to content

Instantly share code, notes, and snippets.

@jsbonso
Last active October 14, 2017 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbonso/f4f7e5b89ea749825658b7d0d6bb47cb to your computer and use it in GitHub Desktop.
Save jsbonso/f4f7e5b89ea749825658b7d0d6bb47cb to your computer and use it in GitHub Desktop.
Java Blackjack Checker
/**
* 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