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

Thread: I need some analysis on a 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 some analysis on a project.

    Here is my homework assignment.

    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


    /**
     *  
     * 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;
     
            roll = 0;
            point_Roll = 0;
     
            Scanner scan = new Scanner(System.in);    
     
            System.out.println( " Let's play a game of dice. " );
            System.out.print( " 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;
     
     
            do
            {
     
            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!");  
            }
            else if( sumdie == 1 || sumdie == 4 || sumdie == 5 || sumdie == 6 || sumdie == 8 || sumdie == 9 || sumdie == 10)
            {
            roll = sumdie;
            }
     
            do 
            {
     
            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 
            {
            System.out.println( " You rolled a" + point_Roll);
            System.out.println( " Roll again! " );
            System.out.print( " Enter \"0\" zero to roll the dice. " );
            roll = scan.nextInt();
            }
     
            } while ( point_Roll != roll)    
     
     
     
     
     
     
     
     
     
            }while ( sumdie != point_Roll);
     
     
     
     
     
     
     
        }    
     
     
     
     
    }


    Okay I'm little confuse on what I should do next. Or if I'm going upon this the right way. My next step is to obvious roll the dice until it reaches 7 or its point roll. How should I go about this?

    Thanks Again!


  2. #2
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: I need some analysis on a project.

    Your assignments comes with an example output, so look at what occurs first because the compiler reads top-down. The first thing is to ask the user if they want to roll by entering 0, otherwise they don't play. As you can see from the example output, if the player wins, they are asked if they want to play again, so you should think whether asking the user should be in a loop or not. In order for the dice to keep being rolled, it has to be in a loop, which you did. Your logic gets redundant with your second do-while loop because consider how the problem is structured. If the player rolls a 2,3,11 or 12, the loop ends, prints to the console and asks if they want to play again. Otherwise, for any other value other than 0, they get a point and the loop re-iterates.

Similar Threads

  1. Quick Code analysis please
    By svo in forum Java Theory & Questions
    Replies: 9
    Last Post: April 7th, 2012, 12:34 AM
  2. Big O - Algorithm Analysis
    By PeskyToaster in forum Java Theory & Questions
    Replies: 4
    Last Post: March 15th, 2012, 11:48 PM
  3. Reading Big File analysis
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: March 9th, 2012, 07:35 AM
  4. Sales Analysis for various products
    By faridzul90 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 22nd, 2011, 08:36 AM
  5. An online source offers project analysis
    By talha06 in forum The Cafe
    Replies: 0
    Last Post: June 20th, 2010, 12:49 PM