0 Replies - 1246 Views - Last Post: 14 October 2012 - 11:35 AM Rate Topic: -----

#1 Schwan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-October 12

The stand option will only work on the first run through

Posted 14 October 2012 - 11:35 AM

When i run this program the hit and stop option alwayse work, while if i type stand it works only on the first run, after that it does nothing.


	
import java.util.Random; 

import javax.swing.JOptionPane;

public class blackjack { 
	static Random rand = new Random();
	static int playerTotal = 0;
    static int dealerTotal = 0;
    String result = "";
    
    public static void main(String[] args) { 
    
    	do {
    		playerTotal = dealOneCard() + dealOneCard();
    		dealerTotal = dealerOneCard();
    		
    		boolean busted = false; String answer;
    		do {
    			
    			
    			if (playerTotal == 21)	
    				JOptionPane.showInputDialog(null, "Blackjack! You win!", JOptionPane.QUESTION_MESSAGE);
    				
    			answer = JOptionPane.showInputDialog(null, playerTotal + "\n" + " Type hit to hit, stand to stand, and stop to end the game " , JOptionPane.QUESTION_MESSAGE);
    				
    			if (answer.equalsIgnoreCase("hit")) playerTotal = playerTotal + 	dealOneCard();
    			else if (answer.equalsIgnoreCase("stop")) 
        			System.exit(0);
    			if (answer.equalsIgnoreCase("stand"))
        				if (playerTotal > dealerTotal)
        					
        					JOptionPane.showInputDialog(null, "The player wins with a " + playerTotal + "\n" + "with the dealer having " + dealerTotal, JOptionPane.QUESTION_MESSAGE);
        				else if (dealerTotal > playerTotal && dealerTotal < 22)
        					
        					JOptionPane.showInputDialog(null, "The dealer wins with a " + dealerTotal + "\n" + " Over your " + playerTotal, JOptionPane.QUESTION_MESSAGE);
    			
    		}
    	
    	while(playerTotal <= 21);
    		
    		if (playerTotal > 21)
    			JOptionPane.showInputDialog(null, "Your busted at " + playerTotal);
    		
		
    	}
    	
    	while(playerTotal <= 100);	
    	
    	 
    	}

    public static int dealerOneCard() {
		
		
		int r = (int) (11*rand.nextDouble()+1);
		dealerTotal = dealerTotal + r;
		do{
			if (dealerTotal < 17)
				dealerTotal = dealerTotal + r;
			
		}
		while (dealerTotal < 17);
		
		
		
	return dealerTotal; }	
    
   

	public static int dealOneCard() {
		
			int r = (int)(11*rand.nextDouble()+1);
	
 

		return r;
	}
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1