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

Thread: I need help with a for loop project!

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default I need help with a for loop project!

    Here is my project!

    To play craps, a player rolls two dice repeatedly until he wins or loses.
    If he makes a 7 or an 11 on the first roll, he wins immediately.
    An initial roll of 2, 3, or 12 results in a loss.
    If he tosses a 4, 5, 6, 8, 9, or 10 on his first roll, then that number becomes his “point”.
    After a player makes a point, he continues rolling the dice and wins or loses according to the
    following rules: if he makes his point before rolling a seven, he wins;
    but if he rolls a seven first, he loses.
    No other values, including 2, 3, 11 or 12, affect the game’s outcome once the player has established his point.
    Write a program that plays craps. Your program should allow a user to play more than one game. Typical output appears below:
    Enter 0 to roll the dice: 0
    You rolled a 7
    You win
    Play again? Enter 1 for yes: 1
    Enter 0 to roll the dice: 0
    You rolled a 4.
    Your point is 4. Continue rolling.
    Enter 0 to roll the dice: 0
    You rolled a 3
    Enter 0 to roll the dice: 0
    You rolled a 5
    Enter 0 to roll the dice: 0
    You rolled a 7
    You lose
    Play again? Enter 1 for yes: 0
    Bye
    Hint: To roll a single die, generate a random number between 1 and 6 inclusive. You can do this with (int)(6 * Math.random()) + 1



    /**
     *  
     * Author: Gregory B Shavers 
     * CSC- 225 Online
     * Lab 5
     */
     
    import java.util.*;
     
    public class lab5
    {
     
     
     
        public static void main ( String[] args ) 
        {
     
            int roll, point_Roll , die1, die2, sumdie, answer;
     
            answer = 0;
            roll = 1;
            point_Roll = 2;
     
            Scanner scan = new Scanner(System.in);    
     
     
            do
            {
     
            System.out.println( " Let's play a game of dice. " );
            System.out.println( " Enter \"0\" zero to roll the dice. " );
            roll = scan.nextInt();
     
            die1 = (int)(6*Math.random())+1;
            die2 = (int)(6*Math.random())+1;
     
            sumdie = die1 + die2;
     
     
     
                if ( sumdie == 2 || sumdie == 3 || sumdie == 12)
                    {
                        System.out.println( " You rolled a " + sumdie);    
                        System.out.println( " You lose! ");   
                     }
     
     
     
               else if  ( sumdie == 7 || sumdie == 11)
                   {
                       System.out.println( " You rolled a " + sumdie );    
                       System.out.println( " You win! "); 
                       System.out.println( "" );  
                    }
     
     
     
     
               else if( sumdie == 1 || sumdie == 4 || sumdie == 5 || sumdie == 6 || sumdie == 8 || sumdie == 9 || sumdie == 10)
                {
                    System.out.println(" ");
                    System.out.println( " You rolled a " + sumdie );
                    System.out.println( " This is your point roll: " + sumdie );
                    System.out.println( " " ); 
                    do 
                    {
                        System.out.println( " Please enter \"0\" zero to roll the dice again. " );
                        System.out.println( " You rolled a: " + point_Roll );
                        point_Roll = scan.nextInt();
                        System.out.println( " " );      
     
                        die1 = (int)(6*Math.random())+1;
                        die2 = (int)(6*Math.random())+1;
     
                        point_Roll = die1 + die2;
     
                            if(point_Roll == 7)
                            {
                                System.out.println( " You rolled a " + point_Roll );
                                System.out.println( " You lose! " );
                            }
     
                                    else if (point_Roll == sumdie)
                                    {
                                        System.out.println( " You rolled a " + point_Roll);
                                        System.out.println( " You win! " );
                                    }
     
     
     
     
                 }while ( point_Roll != sumdie || point_Roll != 7 );
     
                }
     
             do
             {
             System.out.println( " Play again? 1 for YES; 0 for NO: " );    
             roll = scan.nextInt();
             System.out.print(" ");
             }while( roll != 1 && answer != 0);
     
            }while( roll == 1 );
     
       System.out.println( " Thanks for playing. " );
     
     
    }
     
    }

    Here is my output below.
    Let's play a game of dice.
    Enter "0" zero to roll the dice.
    0

    You rolled a 4
    This is your point roll: 4

    Please enter "0" zero to roll the dice again.
    You rolled a: 0
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 12
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 6
    0

    You rolled a 7 You lose!
    Play again? 1 for YES; 0 for NO: 1
    Please enter "0" zero to roll the dice again.
    You rolled a: 7
    0

    You rolled a 7 You lose!
    Play again? 1 for YES; 0 for NO: 1
    Please enter "0" zero to roll the dice again.
    You rolled a: 7
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 8
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 6
    0

    You rolled a 4
    You win!
    Play again? 1 for YES; 0 for NO:


    I know I made a fundamental error but I don't know what. Can any one please help??

    Thanks Again!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help with a for loop project!

    Did you post the code that created the output that is posted here?
    I don't ever get a value > 12 in the "You rolled a:" message

    You forgot to say that you are entering numbers next to the numbers printed out by the program. You need to use println() to make sure the the output is on its own line.

    The logic of the code is very hard to follow because of the poor formatting.
    Please edit the code and properly indent nested statements within {}s to make it easier to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with a for loop project!

    Yeah I'm really sorry. These for loops are really hard for me to format. I was having trouble reading it myself. Can we just start off with formatting? Cause I really don't know who to make it more readable. I don't know what you mean by {}? Could you elaborate? Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help with a for loop project!

    Here's a quick example:
    void method() {
        if(cond) {
           do {
             something;
           }while();
        } // end if
    } // end method()

    The start of the line with the opening {
    is directly above the line with the closing }

    There should NOT be two } one directly below the other like at the end of your code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with a for loop project!

    Okay I just reformatted my code to make it allot more easier for both of us. Okay I see a problem here.

    Please enter "0" zero to roll the dice again.
    You rolled a: 8
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 6
    0

    You rolled a 4
    You win!
    Play again? 1 for YES; 0 for NO:

    For some reason after I start a new game it immediately rolls the dice again instead of restarting the loop. The loop never really successfully go through its process. It just continues the loop until it reaches its original point value which was 4 in its case. I want to start over but I don't know what I did wrong. Do you know why my loop is not starting over again???

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help with a for loop project!

    after I start a new game it immediately rolls the dice again instead of restarting the loop.
    Where is that shown in what you posted for the program's output?
    What should the output look like if what you posted shows a wrong output?


    The formatting is still messed up. For example here
            }while ( point_Roll != sumdie || point_Roll != 7 );
     
            }
    The }s are in the same column. There should be indentation for the upper one.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with a for loop project!

    Okay I re written my code again. I here is the output now.

    Output
    You rolled a 2
    You lose!
    Play again? 1 for YES; 0 for NO:
    1
    Let's play a game of dice.
    Enter "0" zero to roll the dice.
    0

    You rolled a 10
    This is your point roll: 10

    Please enter "0" zero to roll the dice again.
    You rolled a: 2
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 6
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 6
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 3
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 5
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 11
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 8
    0

    Please enter "0" zero to roll the dice again.
    You rolled a: 9
    0

    You rolled a 10
    You win!
    Please enter "0" zero to roll the dice again.
    You rolled a: 10


    As you can see the for loop is going allot better now. BUTfor some reason the game did not start over. Please look below after hitting my point roll 10. Instead of starting over the game, it continues the game. And use my point roll 10 again until I loose or hit 10 again. Is that more clear NORM?

    --- Update ---

    I have a gut feeling that this last java statement is wrong.

    if(point_Roll == 7)
                            {
                                System.out.println( " You rolled a " + point_Roll );
                                System.out.println( " You lose! " );
                            }
     
                                    else if (point_Roll == sumdie)
                                    {
                                        System.out.println( " You rolled a " + point_Roll);
                                        System.out.println( " You win! " );
                                    }
     
     
     
     
                 }while ( point_Roll != sumdie || point_Roll != 7 );

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help with a for loop project!

    for some reason the game did not start over.
    You need to add some comments to the program's output that shows where the problem is. Something like:

    Please enter "0" zero to roll the dice again.
    You rolled a: 3
    0

    Please enter "0" zero to roll the dice again. <<<<<<<<<<<< Here the message should have been ....
    You rolled a: 5
    0


    A suggestion to make the code easier to debug. There are too many messages with the same text printed in different places. You can not tell where the message was printed. Add a number or letter to each message so they are all unique:
    System.out.println( "1 You rolled a "
    Then 2 and 3 etc so they are all unique.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help with a for loop project!

    Okay I think I know where the problem is. Please allow me to show you. I think after establishing a point value the dice is then rolled. After rolling and eventually hitting 7 or hitting the point value the loop does not discontinue. It keeps looping in the part of the code.

    else if( sumdie == 1 || sumdie == 4 || sumdie == 5 || sumdie == 6 || sumdie == 8 || sumdie == 9 || sumdie == 10)
                {
                    System.out.println(" ");
                    System.out.println( " You rolled a " + sumdie );
                    System.out.println( " This is your point roll: " + sumdie );
                    System.out.println( " " ); 
                    do 
                    {
                        System.out.println( " Please enter \"0\" zero to roll the dice again. " );
                        System.out.println( " You rolled a: " + point_Roll );
                        point_Roll = scan.nextInt();
                        System.out.println( " " );      
     
                        die1 = (int)(6*Math.random())+1;
                        die2 = (int)(6*Math.random())+1;
     
                        point_Roll = die1 + die2;
     
                            if(point_Roll == 7)
                            {
                                System.out.println( " You rolled a " + point_Roll );
                                System.out.println( " You lose! " );
                            }
     
                                    else if (point_Roll == sumdie)
                                    {
                                        System.out.println( " You rolled a " + point_Roll);
                                        System.out.println( " You win! " );
                                    }


    Output


    Let's play a game of dice.
    Enter "0" zero to roll the dice.
    0

    You rolled a 8
    This is your point roll: 8

    Please enter "0" zero to roll the dice again.
    You rolled a: 2
    0

    You rolled a 8
    You win!
    Please enter "0" zero to roll the dice again. //for loop should start over instead of re looping
    You rolled a: 8

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I need help with a for loop project!

    last java statement is wrong.
    If you are talking about the while statement, try writing a 10 line program with a do{} while loop with a condition like that statement. Add some println statements in and after the loop to see what happens and have a couple of variables that are used in the while. Try compiling and executing the code with lots of different values in the variables to see what happens. Try many different combinations.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  5. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM