Results 1 to 4 of 4
  1. #1
    Bitterguy is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Random variables are the same, whent they should be all different.

    I am making a black jack program, as part of my goal to design and write at least one program a week, and I made 4 random variables with while loops to try and stop them from all being the same, and I cant figure out what I'm doing wrong, please help, thank you.

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Blackjack {
    //ace = 1,14,27,40
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		Random generator = new Random();
    		int playerDown = generator.nextInt(52) + 1;
    		String PD = "0";
    		int playerUp = generator.nextInt(52) + 1;
    		String PU = "0";
    		int dealerDown = generator.nextInt(52) + 1;
    		String DD = "0"; //for testing purposes
    		int dealerUp = generator.nextInt(52) + 1;
    		String DU = "0";
    		
    		System.out.print("Please enter initial bet in whole dollars: ");
    		double iBet = input.nextInt();
    		
    		while (playerUp == playerDown || playerUp == dealerUp || playerUp == dealerDown) { //trying to prevent them from being the same
    			playerUp = generator.nextInt(52) + 1;
    		}
    		while (dealerUp == dealerDown || dealerUp == playerUp || dealerUp == playerDown) {
    			dealerUp = generator.nextInt(52) + 1;
    		}
    		while (playerDown == playerUp || playerDown == dealerUp || playerDown == dealerDown) {
    			playerDown = generator.nextInt(52) + 1;
    		}
    		while (dealerDown == playerDown || dealerDown == dealerUp || dealerDown == playerDown) {
    			dealerDown = generator.nextInt(52) + 1;
    		}
    		
    		switch(playerUp){ // determines which number is which card from the above variables
    		case 1: PU = "Ace of Spades";
    		case 2: PU = "Two of Spades";
    		case 3: PU = "Three of Spades";
    		case 4: PU = "Four of Spades";
    		case 5: PU = "Five of Spades";
    		case 6: PU = "Six of Spades";
    		case 7: PU = "Seven of Spades";
    		case 8: PU = "Eight of Spades";
    		case 9: PU = "Nine of Spades";
    		case 10: PU = "Ten of Spades";
    		case 11: PU = "Jack of Spades";
    		case 12: PU = "Queen of Spades";
    		case 13: PU = "King of Spades";
    		case 14: PU = "Ace of Hearts";
    		case 15: PU = "Two of Hearts";
    		case 16: PU = "Three of Hearts";
    		case 17: PU = "Four of Hearts";
    		case 18: PU = "Five of Hearts";
    		case 19: PU = "Six of Hearts";
    		case 20: PU = "Seven of Hearts";
    		case 21: PU = "Eight of Hearts";
    		case 22: PU = "Nine of Hearts";
    		case 23: PU = "Ten of Hearts";
    		case 24: PU = "Jack of Hearts";
    		case 25: PU = "Queen of Hearts";
    		case 26: PU = "King of Hearts";
    		case 27: PU = "Ace of Clubs";
    		case 28: PU = "Two of Clubs";
    		case 29: PU = "Three of Clubs";
    		case 30: PU = "Four of Clubs";
    		case 31: PU = "Five of Clubs";
    		case 32: PU = "Six of Clubs";
    		case 33: PU = "Seven of Clubs";
    		case 34: PU = "Eight of Clubs";
    		case 35: PU = "Nine of Clubs";
    		case 36: PU = "Ten of Clubs";
    		case 37: PU = "Jack of Clubs";
    		case 38: PU = "Queen of Clubs";
    		case 39: PU = "King of Clubs";
    		case 40: PU = "Ace of Diamonds";
    		case 41: PU = "Two of Diamonds";
    		case 42: PU = "Three of Diamonds";
    		case 43: PU = "Four of Diamonds";
    		case 44: PU = "Five of Diamonds";
    		case 45: PU = "Six of Diamonds";
    		case 46: PU = "Seven of Diamonds";
    		case 47: PU = "Eight of Diamonds";
    		case 48: PU = "Nine of Diamonds";
    		case 49: PU = "Ten of Diamonds";
    		case 50: PU = "Jack of Diamonds";
    		case 51: PU = "Qeen of Diamonds";
    		case 52: PU = "King of Diamonds";
    		
    		
    		}
    		switch(playerDown){
    		case 1: PD = "Ace of Spades";
    		case 2: PD = "Two of Spades";
    		case 3: PD = "Three of Spades";
    		case 4: PD = "Four of Spades";
    		case 5: PD = "Five of Spades";
    		case 6: PD = "Six of Spades";
    		case 7: PD = "Seven of Spades";
    		case 8: PD = "Eight of Spades";
    		case 9: PD = "Nine of Spades";
    		case 10: PD = "Ten of Spades";
    		case 11: PD = "Jack of Spades";
    		case 12: PD = "Queen of Spades";
    		case 13: PD = "King of Spades";
    		case 14: PD = "Ace of Hearts";
    		case 15: PD = "Two of Hearts";
    		case 16: PD = "Three of Hearts";
    		case 17: PD = "Four of Hearts";
    		case 18: PD = "Five of Hearts";
    		case 19: PD = "Six of Hearts";
    		case 20: PD = "Seven of Hearts";
    		case 21: PD = "Eight of Hearts";
    		case 22: PD = "Nine of Hearts";
    		case 23: PD = "Ten of Hearts";
    		case 24: PD = "Jack of Hearts";
    		case 25: PD = "Queen of Hearts";
    		case 26: PD = "King of Hearts";
    		case 27: PD = "Ace of Clubs";
    		case 28: PD = "Two of Clubs";
    		case 29: PD = "Three of Clubs";
    		case 30: PD = "Four of Clubs";
    		case 31: PD = "Five of Clubs";
    		case 32: PD = "Six of Clubs";
    		case 33: PD = "Seven of Clubs";
    		case 34: PD = "Eight of Clubs";
    		case 35: PD = "Nine of Clubs";
    		case 36: PD = "Ten of Clubs";
    		case 37: PD = "Jack of Clubs";
    		case 38: PD = "Queen of Clubs";
    		case 39: PD = "King of Clubs";
    		case 40: PD = "Ace of Diamonds";
    		case 41: PD = "Two of Diamonds";
    		case 42: PD = "Three of Diamonds";
    		case 43: PD = "Four of Diamonds";
    		case 44: PD = "Five of Diamonds";
    		case 45: PD = "Six of Diamonds";
    		case 46: PD = "Seven of Diamonds";
    		case 47: PD = "Eight of Diamonds";
    		case 48: PD = "Nine of Diamonds";
    		case 49: PD = "Ten of Diamonds";
    		case 50: PD = "Jack of Diamonds";
    		case 51: PD = "Qeen of Diamonds";
    		case 52: PD = "King of Diamonds";
    		
    		
    		}
    		switch(dealerUp){
    		case 1: DU = "Ace of Spades";
    		case 2: DU = "Two of Spades";
    		case 3: DU = "Three of Spades";
    		case 4: DU = "Four of Spades";
    		case 5: DU = "Five of Spades";
    		case 6: DU = "Six of Spades";
    		case 7: DU = "Seven of Spades";
    		case 8: DU = "Eight of Spades";
    		case 9: DU = "Nine of Spades";
    		case 10: DU = "Ten of Spades";
    		case 11: DU = "Jack of Spades";
    		case 12: DU = "Queen of Spades";
    		case 13: DU = "King of Spades";
    		case 14: DU = "Ace of Hearts";
    		case 15: DU = "Two of Hearts";
    		case 16: DU = "Three of Hearts";
    		case 17: DU = "Four of Hearts";
    		case 18: DU = "Five of Hearts";
    		case 19: DU = "Six of Hearts";
    		case 20: DU = "Seven of Hearts";
    		case 21: DU = "Eight of Hearts";
    		case 22: DU = "Nine of Hearts";
    		case 23: DU = "Ten of Hearts";
    		case 24: DU = "Jack of Hearts";
    		case 25: DU = "Queen of Hearts";
    		case 26: DU = "King of Hearts";
    		case 27: DU = "Ace of Clubs";
    		case 28: DU = "Two of Clubs";
    		case 29: DU = "Three of Clubs";
    		case 30: DU = "Four of Clubs";
    		case 31: DU = "Five of Clubs";
    		case 32: DU = "Six of Clubs";
    		case 33: DU = "Seven of Clubs";
    		case 34: DU = "Eight of Clubs";
    		case 35: DU = "Nine of Clubs";
    		case 36: DU = "Ten of Clubs";
    		case 37: DU = "Jack of Clubs";
    		case 38: DU = "Queen of Clubs";
    		case 39: DU = "King of Clubs";
    		case 40: DU = "Ace of Diamonds";
    		case 41: DU = "Two of Diamonds";
    		case 42: DU = "Three of Diamonds";
    		case 43: DU = "Four of Diamonds";
    		case 44: DU = "Five of Diamonds";
    		case 45: DU = "Six of Diamonds";
    		case 46: DU = "Seven of Diamonds";
    		case 47: DU = "Eight of Diamonds";
    		case 48: DU = "Nine of Diamonds";
    		case 49: DU = "Ten of Diamonds";
    		case 50: DU = "Jack of Diamonds";
    		case 51: DU = "Qeen of Diamonds";
    		case 52: DU = "King of Diamonds";
    		
    		
    		}
    		switch(dealerDown){
    		case 1: DD = "Ace of Spades";
    		case 2: DD = "Two of Spades";
    		case 3: DD = "Three of Spades";
    		case 4: DD = "Four of Spades";
    		case 5: DD = "Five of Spades";
    		case 6: DD = "Six of Spades";
    		case 7: DD = "Seven of Spades";
    		case 8: DD = "Eight of Spades";
    		case 9: DD = "Nine of Spades";
    		case 10: DD = "Ten of Spades";
    		case 11: DD = "Jack of Spades";
    		case 12: DD = "Queen of Spades";
    		case 13: DD = "King of Spades";
    		case 14: DD = "Ace of Hearts";
    		case 15: DD = "Two of Hearts";
    		case 16: DD = "Three of Hearts";
    		case 17: DD = "Four of Hearts";
    		case 18: DD = "Five of Hearts";
    		case 19: DD = "Six of Hearts";
    		case 20: DD = "Seven of Hearts";
    		case 21: DD = "Eight of Hearts";
    		case 22: DD = "Nine of Hearts";
    		case 23: DD = "Ten of Hearts";
    		case 24: DD = "Jack of Hearts";
    		case 25: DD = "Queen of Hearts";
    		case 26: DD = "King of Hearts";
    		case 27: DD = "Ace of Clubs";
    		case 28: DD = "Two of Clubs";
    		case 29: DD = "Three of Clubs";
    		case 30: DD = "Four of Clubs";
    		case 31: DD = "Five of Clubs";
    		case 32: DD = "Six of Clubs";
    		case 33: DD = "Seven of Clubs";
    		case 34: DD = "Eight of Clubs";
    		case 35: DD = "Nine of Clubs";
    		case 36: DD = "Ten of Clubs";
    		case 37: DD = "Jack of Clubs";
    		case 38: DD = "Queen of Clubs";
    		case 39: DD = "King of Clubs";
    		case 40: DD = "Ace of Diamonds";
    		case 41: DD = "Two of Diamonds";
    		case 42: DD = "Three of Diamonds";
    		case 43: DD = "Four of Diamonds";
    		case 44: DD = "Five of Diamonds";
    		case 45: DD = "Six of Diamonds";
    		case 46: DD = "Seven of Diamonds";
    		case 47: DD = "Eight of Diamonds";
    		case 48: DD = "Nine of Diamonds";
    		case 49: DD = "Ten of Diamonds";
    		case 50: DD = "Jack of Diamonds";
    		case 51: DD = "Qeen of Diamonds";
    		case 52: DD = "King of Diamonds";
    		
    		
    		}
    		
    		System.out.println("Dealer is showing the " + DU);
    		System.out.println("Dealer is showing the " + DD); // testing purposes
    		System.out.println("Your are hiding the " + PD + " and showing the " + PU);
    		if (playerUp == 1 || playerUp == 14 || playerUp == 27 || playerUp == 40) {
    			System.out.print("You have an ace, do you want it to be worth one or eleven: ");
    			int pAceOne = input.nextInt();
    		}
    
    	}
    and it results:
    Please enter initial bet in whole dollars: 1
    Dealer is showing the King of Diamonds
    Dealer is showing the King of Diamonds
    Your are hiding the King of Diamonds and showing the King of Diamonds

    i even tried changing it to generator.next.Int(1) + 1 but it still happened.

    Edit: after some further testing I found out that it always goes to the highest case
    Last edited by Bitterguy; 05-28-2012 at 01:47 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Random variables are the same, whent they should be all different.

    Can you make a small, simple program that compiles, executes and shows the problem by printing out the values that are "all being the same"? Post the code and its printed output.

    Is your problem with the switch statement? You need to use a break statement to keep execution from falling through to the next case.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Random variables are the same, whent they should be all different.

    You forgot to add a 'break' at the end of each case clause (cases fall through in Java and C++ and C)

    kind regards,

    Jos

    edit: I should've refreshed the page after I woke up ...
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Bitterguy is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: Random variables are the same, whent they should be all different.

    Quote Originally Posted by JosAH View Post
    You forgot to add a 'break' at the end of each case clause (cases fall through in Java and C++ and C)

    kind regards,

    Jos

    edit: I should've refreshed the page after I woke up ...
    Gahhh, thanks, I wanna kick myself after that one, I haven't messed with java since my class first semester haha. Gotta work the rust off :P

Similar Threads

  1. Replies: 0
    Last Post: 01-23-2012, 09:12 AM
  2. random to take variables
    By javajames in forum New To Java
    Replies: 9
    Last Post: 05-15-2011, 07:32 PM
  3. Replies: 17
    Last Post: 07-25-2010, 07:20 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. Replies: 8
    Last Post: 04-19-2009, 05:50 PM

Posting Permissions

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