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 8 of 8

Thread: can some one fix my black jack code

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default can some one fix my black jack code

    import java.util.Scanner;

    public class Deck {

    public static void main(String[] args) {
    int deal=(int)(11*Math.random()+1);
    int deal1=(int)(11*Math.random()+1);
    int deal2=(int)(11*Math.random()+1);
    int deal3=(int)(11*Math.random()+1);


    Scanner input = new Scanner(System.in);

    int sum;
    int sum1;
    int player;
    int dealer;

    // deal two cards to the player
    System.out.println( "your cards are:");
    System.out.println( player=deal);
    System.out.println(player=deal1);
    System.out.println("your total cards are:");
    System.out.println(sum1=deal+deal1);

    dealer=deal2;
    dealer=deal3;
    sum=deal2+deal3;

    if(sum1==21&&sum==21){
    System.out.println("The player wins");
    }
    else if (sum==21){
    System.out.println("dealer wins");
    }
    else if(sum1==21){
    System.out.println("Player wins");

    }


    // variables for when each player is finished hitting
    boolean iamdone = false;
    boolean dealerDone = false;
    String answer;

    while(!iamdone || !dealerDone) {

    // player's turn
    while(!iamdone){
    System.out.print("Hit or Stay? (Enter H or S): ");
    answer = input.nextLine();


    // if the player hits
    if(answer.equals("H")) {

    // add next card and store whether we've busted
    int deal4=(int)(11*Math.random()+1);

    System.out.println(player=deal4);
    System.out.println("your total is");
    System.out.println(sum1=deal+deal1+deal4);
    }
    else if(sum1==21){
    System.out.println("player wins");
    }
    else if(sum1>21){
    System.out.println("player score over 21, you lose");
    }
    else{
    System.out.println("Dealers turn");
    }

    }

    // dealer's turn
    dealerDone=sum<17;
    while(!dealerDone){


    int deal5=(int)(11*Math.random()+1);

    System.out.println("The Dealer hits");
    dealer=deal5;
    sum=deal2+deal3+deal5;

    if (sum==21){
    System.out.println("dealer wins");
    }
    else if(sum>21){
    System.out.println("dealer score over 21, player wins");
    }


    else{
    System.out.println("dealer stays");


    }



    }
    }

    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: can some one fix my black jack code

    can some one fix my black jack code
    Fix what? Does it compile? Exceptions? Misbehave? Post an SSCCE, and ask a specific question about your code and you'll have much better luck at getting help.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: can some one fix my black jack code

    it does not have to be any thing advanced. its supposed to be computer(dealer) vs. player. you have to deal #'s from 1-11.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: can some one fix my black jack code

    Quote Originally Posted by asad1221 View Post
    it does not have to be any thing advanced. its supposed to be computer(dealer) vs. player. you have to deal #'s from 1-11.
    I still have yet to see you ask a specific question, or answer the questions I posted above. Perhaps someone else might be able to read your mind (or see your computer screen), but to date I don't seem to have that kind of power.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: can some one fix my black jack code

    my while loop is not working properly for the player. if i click hit first time it adds up score properly and everything but if i click hit again than it ignores the old number and only adds the new number

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: can some one fix my black jack code

    Quote Originally Posted by asad1221 View Post
    my while loop is not working properly for the player. if i click hit first time it adds up score properly and everything but if i click hit again than it ignores the old number and only adds the new number
    Again, I'm not good at reading minds. If you want help, you are going to have to put in some effort asking your question so that the question can be understood - and possibly answered.
    Last edited by copeg; February 12th, 2014 at 06:21 PM. Reason: In response to original post edit

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: can some one fix my black jack code

    my while loop is not working properly for the player. if i click hit first time it adds up score properly and everything but if i click hit again than it ignores the old number and only adds the new number

    --- Update ---

    import java.util.Scanner;

    public class Deck {

    public static void main(String[] args) {
    int deal=(int)(11*Math.random()+1);
    int deal1=(int)(11*Math.random()+1);
    int deal2=(int)(11*Math.random()+1);
    int deal3=(int)(11*Math.random()+1);


    Scanner input = new Scanner(System.in);

    int sum;
    int sum1;
    int player;
    int dealer;

    // deal two cards to the player
    System.out.println( "your cards are:");
    System.out.println( player=deal);
    System.out.println(player=deal1);
    System.out.println("your total cards are:");
    System.out.println(sum1=deal+deal1);

    dealer=deal2;
    dealer=deal3;
    sum=deal2+deal3;

    if(sum1==21&&sum==21){
    System.out.println("The player wins");
    }
    else if (sum==21){
    System.out.println("dealer wins");
    }
    else if(sum1==21){
    System.out.println("Player wins");

    }


    // variables for when each player is finished hitting
    boolean iamdone = false;
    boolean dealerDone = false;
    String answer;

    while(!iamdone || !dealerDone) {

    // player's turn
    while(!iamdone){
    System.out.print("Hit or Stay? (Enter H or S): ");
    answer = input.nextLine();


    // if the player hits
    if(answer.equals("H")) {

    // add next card and store whether we've busted
    int deal4=(int)(11*Math.random()+1);

    System.out.println(player=deal4);
    System.out.println("your total is");
    System.out.println(sum1=deal+deal1+deal4);
    }
    else if(sum1==21){
    System.out.println("player wins");
    }
    else if(sum1>21){
    System.out.println("player score over 21, you lose");
    }
    else{
    System.out.println("Dealers turn");
    }

    }

    // dealer's turn
    dealerDone=sum<17;
    while(!dealerDone){


    int deal5=(int)(11*Math.random()+1);

    System.out.println("The Dealer hits");
    dealer=deal5;
    sum=deal2+deal3+deal5;

    if (sum==21){
    System.out.println("dealer wins");
    }
    else if(sum>21){
    System.out.println("dealer score over 21, player wins");
    }


    else{
    System.out.println("dealer stays");


    }



    }
    }

    }
    }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: can some one fix my black jack code

    Please post your code correctly. This topic will show how to post code in code or highlight tags along with other useful info for newcomers.

Similar Threads

  1. Black Jack for dr. java
    By asad1221 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2014, 01:42 AM
  2. Black Jack: How would it look?
    By Javaconfused80850 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2013, 06:22 PM
  3. black jack game
    By 314159 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 6th, 2011, 10:42 AM
  4. (URGENT) Plz help me with the black jack program.
    By diogo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 04:08 AM
  5. Black Jack Statistics game? lost.
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2010, 07:20 AM