BlackJack II
Collapse Content
Show Content
You're writing a program to play a variety of BlackJack. In general, given two numbers, a
and b
, return their sum.
If the sum is greater than 21
, return 0
, unless one of the numbers is 11
. In such a case, the 11
should be 'converted' to a 1
to prevent the sum from being exceeded.
For example, given a 11
and 13
as input, the 11
should be 'converted' into a 1
so the total sum will be 14
.
(See additional input/output below.)
image from wikipedia
Challenge
Return the sum of two blackjack 'cards', as above.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Matt
Nov 15, 5:43 PMI'm lost. My code is showing incorrect, but my results look correct. All my outputs match except for "Other Input/Other Output."
Learneroo
Nov 16, 6:30 PM@Matt, an 11 should only be changed to a 1 if it will exceed 21. In your code, when
b
is 11 it gets turned into a 1 no matter what. This is because&&
is evaluated in Java before||
. See the table http://www.learneroo.com/courses/11/nodes/103#sts=All Order of Operations. You should add parentheses around your code to fix it:Matt
Nov 16, 10:21 PMPerfect, makes sense. Thanks!
Кирилл Варивода
Jan 5, 3:06 PMIt's really simple but not boring, great!
Eduardo Bonet
Jun 4, 12:58 AMBug on last entry with javascript for both BlackJack 1 and BlackJack 2: when read the last line I get "7 11", but after I use parseInt my function receives 7 and 1.
Borja Panadero
Dec 9, 2:25 PMif 11 is converted to 1, last correct output is wrong.
Learneroo
Dec 9, 2:30 PM@Borja, it is only converted to 1 if it will otherwise overshoot 21.
Borja Panadero
Dec 9, 3:31 PMShame on me :) got totally focused on converting 11 to 1. Thanks for pointing out
Cameron Guyer
May 2, 10:24 PMCan't figure out how to convert the correct output to 13 and 14 with the correct code. Everything I tried does a compilation error.
thales
Jul 15, 12:30 PMLets say i have 11 and 11. then automaticly it wil return 12. but perhaps i am a weird player of somekind and i want it to be 2. In the casino i can choose this if i want, here i cant?
my code
ani2fun
Nov 26, 3:36 PMwhy compilation fails error ? my code
Prateek
May 12, 4:23 AMif((a+b)<22 && a!=11 && b!=11){
return a+b;
}
if(a==11 || b==11){
Dharnish
May 20, 9:52 AMvery simple....
return a+b<21?a+b:(a==11||b==11)?a+b-10:0;
vignesh
Jan 29, 1:21 PMreturn a+b>21?(a==11?b+1:(b==11?a+1:0)):a+b;
Roberto Blanco Axelsson
Mar 8, 8:59 AMOne way to do it:
int sum = a+b;
int one = 1;
Hanlise
May 14, 11:28 AMdef do_stuff(a, b):
sum1=a+b
if a ==11:
a=1
This is my code but it's weird. But I got 2 incorrect output