Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: A little help with my Blackjack code

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Location
    Sweden
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    	//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 :]
    Last edited by helloworld922; March 2nd, 2010 at 10:20 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Location
    Sweden
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    //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");
    			}
    		}
    	}

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: A little help with my Blackjack 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.");
    }

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Location
    Sweden
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

Similar Threads

  1. Blackjack program runs excrutiatingly slow.
    By sina12345 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 10th, 2010, 03:55 AM
  2. Creating program Blackjack - Dr.Java
    By TheUntameable in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2010, 11:54 AM