Results 1 to 3 of 3
  1. #1
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default Boolean problems

    Hi my program is supposed to play the game Craps 100,000 times. Basically, I have two dice and if the total of them (after being rolled) is 7, the game is one. If it's a 2 or 3, the game is lost. If it's something else I keep rolling until I get the same number or a 7. My program keeps ending after one roll, no matter what the number is. If it is a 2 or 3, it gets counted as a loss. But everything else is counted as a win so I am only getting one roll. Here is the code for the first roll --

    Java Code:
    public class Craps {
    
    
    
        
        //Plays the craps game
        public static void main(String[] args) {        
            Random random = new Random();
            int round = 1;
            int dieOne;
            int dieTwo;
            int total = 0;
            int point = 0;
            int numberWon = 0;
            int numberLost = 0;
            int rollNumber = 1;
            boolean roundWin = false;
            boolean roundOver = false;
    
            
            while (round <= 100000) {
                roundWin = false;
                roundOver = false;
                rollNumber = 1;
                dieOne = random.nextInt(6) + 1;
                dieTwo = random.nextInt(6) + 1;
                total = dieOne + dieTwo;
                    
                switch(total) {
                   case 2:
                   case 3:
                       roundOver = true;
                       break;
                   case 7:
                       roundWin = true;
                       roundOver = true;
                       break;
                   default:
                       point = total;
                       break;
                   }             
                   if(round <= 10) {
                            System.out.print("Round " + round + ", Roll " + rollNumber);
                            System.out.print(", " + "Die1: " + dieOne );
                            System.out.println(", Die2: " + dieTwo + "-- Total: " + total);                       
                            if(roundOver == true) {
                                if(roundWin == true) {                                
                                    System.out.println("WIN!");  
                                }
                                else if(roundWin == false) {
                                    System.out.println("LOSE!");
                                }
                            }
                    }

    The only way I can have a win and print "WIN!" after the first roll is if roundOver and roundWin are both true. I set them as false after every round and the only time they can be changed to true is when total == 7 on the switch statement. At least that is how I want it to be. Somehow the two booleans are changing to true for every number that isn't 2 or 3. I don't see how. Can anyone offer me any help here? Thanks.

  2. #2
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Oh and I have to display output for the first 10 rounds in case it isn't obvious. But the problem here is the booleans

  3. #3
    Chasingxsuns is offline Member
    Join Date
    Jul 2009
    Posts
    56
    Rep Power
    0

    Default

    Looking at the code again, the problem is later on in the program and not what I originally thought. Apologies for wasted time

Similar Threads

  1. transfer boolean to 1's and 0's
    By Nikohw in forum Java Applets
    Replies: 5
    Last Post: 09-12-2009, 09:05 PM
  2. 1 as an integer, not boolean
    By McChill in forum New To Java
    Replies: 1
    Last Post: 05-02-2009, 09:39 PM
  3. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 05:01 AM
  4. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 05:31 AM
  5. boolean variables
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-31-2007, 04:58 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
  •