Results 1 to 2 of 2
  1. #1
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Blackjack Program Not Calculating Totals Correctly

    I spent a while on this blackjack program. Here's how it doesn't work, This is the console:

    Hello and welcome to blackjack
    My name is david and i'll be your dealer
    ------------------------------------------------------------
    Type Hit me for a card and Hold to stop the dealing
    ------------------------------------------------------------
    Hit me
    Your random card is a 0
    Hit me
    Your random card is a 10
    Hit me
    Your random card is a 4
    Hold
    Your total stays at 14
    You win! score of 4 to 4


    Obviously, problems adding or something
    Here's my code:

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    
    public class BlackJack {
    	  public static void main(String[] args){
    		  Scanner scanner = new Scanner(System.in);
    		  int total = 0;
    		  int dTotal = 0;
    boolean bust = false;
    Init();
    bust = dealerBust(dTotal, bust);
    for(int i=1; i<(25); i++)
    {
    	String input = scanner.nextLine();
    if(input.equalsIgnoreCase("Hit me")){
    
    int random = Random();	 
    System.out.println("Your random card is a " + random);
    
    
    total = random + total;  
    }
    //Check if you went over
    if (total > 21){
    	System.out.println("Sorry, you went over 21");
    	break;
    }
    if(input.equalsIgnoreCase("Hold")){
    	System.out.println("Your total stays at " + total);
    	Hold(bust, i, i);
    	break;
    }
    }
    	
    }
      
    
    
    	  
    	private static boolean dealerBust(int dTotal, boolean bust) {
    		// TODO Auto-generated method stub
    		for(int i=1; i<(25); i++)
    		{
    		int random1 = Random();
    		dTotal = random1 + dTotal;
    		if (dTotal >= 17){
    			bust = false;
    		break;
    		}
    		else if(dTotal > 21)
    		{
    			bust = true;
    		}
    		}
    		return bust;
    		
    	}
    
    
    
    
    	private static void Hold(boolean bust,int total,int dTotal) {
    		if(bust == false){
    			if(total >= dTotal){
    				System.out.println("You win! score of " + total + " to " + dTotal);
    			}
    			else
    				{
    					System.out.println("You loose! score of " + total + " to " + dTotal);
    				}
    			}
    			
    		}
    		
    		
    	
    
    
    
    
    	public static int Random() {
    		// TODO Auto-generated method stub
    		 Random randomGenerator = new Random();
    		 int randomInt = randomGenerator.nextInt(12);
    		return randomInt;
    	}
    
    
    	private static void Init() {
    		System.out.println("Hello and welcome to blackjack");
    		System.out.println("My name is david and i'll be your dealer");
    		System.out.println("------------------------------------------------------------");
    		System.out.println("Type Hit me for a card and Hold to stop the dealing ");
    		System.out.println("------------------------------------------------------------");
    
    		
    	}
    }

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Blackjack Program Not Calculating Totals Correctly

    In line 31:
    Java Code:
    Hold(bust, i, i);
    Why are you sending i? i is just a counter for the number of turns, when it looks like Hold() is supposed to take scores in the last two arguments.

Similar Threads

  1. Need quick help with a Java blackjack program!
    By Enigma in forum New To Java
    Replies: 11
    Last Post: 06-01-2011, 10:59 AM
  2. Why doesn't my program function correctly?
    By robertbob in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 02:03 AM
  3. calculating time of program runing
    By sara12345 in forum New To Java
    Replies: 3
    Last Post: 03-17-2010, 01:25 AM
  4. Help with calculating triangle program
    By ALH813 in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 06:08 AM
  5. Help with simple Blackjack Program?
    By meeper3000 in forum New To Java
    Replies: 2
    Last Post: 04-26-2009, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •