# I need help with a for loop project!

• February 18th, 2013, 05:48 PM
Rain_Maker
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

Code java:

/**
*
* 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;

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:

Thanks Again!
• February 18th, 2013, 05:58 PM
Norm
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.
• February 18th, 2013, 06:15 PM
Rain_Maker
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!
• February 18th, 2013, 06:39 PM
Norm
Re: I need help with a for loop project!
Here's a quick example:
Code :

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.
• February 18th, 2013, 06:53 PM
Rain_Maker
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???
• February 18th, 2013, 07:29 PM
Norm
Re: I need help with a for loop project!
Quote:

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
Code :

}while ( point_Roll != sumdie || point_Roll != 7 );

}

The }s are in the same column. There should be indentation for the upper one.
• February 18th, 2013, 07:49 PM
Rain_Maker
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? :confused:

--- Update ---

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

Code Java:

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 );

• February 18th, 2013, 07:54 PM
Norm
Re: I need help with a for loop project!
Quote:

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:
Code :

System.out.println( "1 You rolled a "

Then 2 and 3 etc so they are all unique.
• February 18th, 2013, 08:06 PM
Rain_Maker
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.

Code Java:

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
• February 18th, 2013, 08:11 PM
Norm
Re: I need help with a for loop project!
Quote:

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.