A little help with my Blackjack code
Hello!
I've been working on this code and it will be really simple. it is supposed to be blackjack look a like but with 1 player that puts in 2 numbers and the one closest to 21 will be presented as winner. Anyone have som ideas to complete this code. i have 1 compilator which is keyboard.class
Anyway, here it is.
Code :
//Blackjack
public class Blackjack{
public static void main(String[] args){
System.out.print("Skriv in tal 1. ");
double tal1 = Keyboard.readDouble();
System.out.print("Skriv in tal 2. ");
double tal2 = Keyboard.readDouble();
if (tal1 == 21 && tal2 != 21){
System.out.println("Tal 1 vann");
}
else if (tal2 == 21 && tal1 != 21){
System.out.print("Tal 2 vann");
}
else if (tal1 < 21 && 21 < tal2){
System.out.print("Tal 1 vann");
}
else {
System.out.print("tal1 vann");
}
}
}
''tal1'' is just a name for one of the numbers to put in. And so far it displays the proper winner if i enter number 21. but ''tal1'' always win no matter which numbers i put in. The thing i want is. i mount a number in booth 'tal1' and 'tal2' and the one closest to 21 is printed out as a winner. It shouldnt be that hard but i am not very good yet.
Any help is much appreciated :]
Re: A little help with my Blackjack code
Add an if statement inside the third block to check if tal1 is bigger or if tal2 is bigger. Also, it's unnecessary to have blocks 1 and 2, simply change block three's if statement to include 21. Lastly, your else block should be printing out that it was a tie game, unless you want tie games to mean tal1 wins.
Re: A little help with my Blackjack code
Hey again, i didnt get what you mean there but i changed some of the code, some errors is still the same but here it is:
Code :
//Blackjack
public class Blackjack{
public static void main(String[] args){
System.out.print("Skriv in tal 1. ");
double tal1 = Keyboard.readDouble();
System.out.print("Skriv in tal 2. ");
double tal2 = Keyboard.readDouble();
if (tal1 == 21 && tal2 != 21){
System.out.println("Tal 1 vann");
}
else(tal2 == 21 && tal1 != 21){
System.out.print("Tal 2 vann");
}
else if (tal1<tal2 && tal2<21){
System.out.println("tal1 vann");
}
else{(tal2<tal1 && tal1<21)
System.out.println("tal2 vann");
}
}
}
Re: A little help with my Blackjack code
Code :
if (tal1 > 21 && tal2 > 21)
{
System.out.println("You both busted!");
}
else if (tal1 > 21)
{
System.out.println("Tal1 busted, Tal2 wins!");
}
else if (tal2 > 21)
{
System.out.println("Tal2 busted, Tal1 wins!");
}
else if (tal1 > tal2)
{
System.out.println("Tal1 wins!");
}
else if (tal2 > tal1)
{
System.out.println("Tal2 wins!");
}
else
{
System.out.println("The scores were tied.");
}
Re: A little help with my Blackjack code
Hmm... that was a good one too, but i added alot of else if's instead, now i just need to add some more math stuff into it to complete it. Thanks alot i'll get working! much appreciated!