Hello,

Thank you in advance for taking the time to help me!!

My assignment is to write a dice game in java using the Die.java class.The object of the game is to reach a total of 41 or more by rolling a pair of dice no more than 6 times. If you get 10, 20,30, or 40 as your total you automatically lose, and if you roll doubles you get an extra roll (however if you roll a 5 & 5 you loose).

I am having problems adding my turn totals !!! please help me.
for instance i get
Roll #1 5 2 Your total is : 7
Roll #2 3 1 Your total is : 7 when it should be 11
Roll #3 2 3 Your total is : 7 when it should be 16 and it keeps doing that for all the rolls

how do i get the turn total to add up after every roll?


I am just a beginner in programming so please be patient with me, and i apologize if didnt make myself too clear.

Thank you again!

public class FortyOne{
	public static void main(String [] args){
		
		
		Die die1, die2;
			
		//Create a pair of dice
		die1 = new Die();	
		die2 = new Die(6);
		
		//Rolls dice
		die1.roll();
		die2.roll();
		int rollCount;
		rollCount = 0;
		rollCount++;
		
		
		
		
		//Initiate game	
		System.out.println("Let's Play 41!!!");
		System.out.print("Roll #"+ rollCount+"  ");
		rollCount++;
		
		// After the roll, prints out the value of die1 & 2.
		System.out.print(die1.getUpValue() + "  ");
		System.out.print(die2.getUpValue() + "  ");
		
		//if you get doubles
		if (die1.getUpValue() == die2.getUpValue())
			System.out.print("YOU GOT DOUBLES!!!   ");
		
	
		
		//Gives turn total
		int turnTotal = die1.getUpValue()+die2.getUpValue();

		//prints out sum of die1 and die2
		System.out.print("Your total is: " + turnTotal+  "  ");
		
		//Adds turnTotal to the next ?????
		int playerTotal =0;
		playerTotal+=turnTotal;
	
		
		//gives the wining number
		int winner= 41;
		
		//Limits the amount of rolls to 6.
		if(rollCount >=7)
		System.out.println("YOU LOSE!!");
		
		//Tells player they lose if their roll amount or total equals 10,20,30,or 40
		if(turnTotal == 10 || turnTotal == 20 || turnTotal == 30 || turnTotal == 40){
			System.out.println("\n" +" AW!! You lose! Sorry!");}
		else if(playerTotal == 10 || playerTotal == 20 || playerTotal == 30 || playerTotal == 40){
			System.out.println("\n" +" AW!! You lose! Sorry!");}
			else 		
			
		do {
			System.out.print("\n" + "Roll #"+ rollCount+"  ");
			rollCount++;			
			die1.roll();
			die2.roll();
			die1.getUpValue();
			die2.getUpValue();
			System.out.print(die1.getUpValue() + "  ");
			System.out.print(die2.getUpValue() + "  ");
			System.out.print("Your total is: " + (turnTotal)+  "  ");
		} while (rollCount<=6);
rollCount++;	
	
	}
	
}
public class Die
{	
	private int numFaces; 
	private int upValue;
	
	
	public Die()
	{	
		numFaces = 6;
		upValue = 1;
	}

	public Die(int faces)
	{	
		numFaces = faces;
		upValue = 1;
	}

	public void roll()
 	{	
		upValue = ((int)(Math.random() * numFaces)) + 1; 
 	}
	 

	
	public int getUpValue() 
	{	
		return upValue;	
	}
}

You need to save the previous total and add to it the new total.

Where do you compute the total that is displayed in error? Where do you update it?

I know that's what I need to do I just don't know how :/.
The first roll adds up no problem but the following rolls are the problem.

What value do you want to have in turnTotal the second time you print it?
Where are the sub values you need to add together to get that value?

For instance this is the way it should be
Roll#1 5 2 Your Total= 7
Roll#2. 3. 1. Your total= 11. (so basically turnTotal should add 7 for the first roll and the sum of the second roll, and it should continue to do that for all 6 rolls)
Bare with me I don't know if in explaining it right.

Ok that looks right.
Now write the code to do that after the next roll where the dice have been rolled.
turnTotal has the total up to that point.

Edited 5 Years Ago by NormR1: n/a

Write an assignment statement that adds up those three values(current value of turnTotal, dice1 value and dice2 value) and puts the value in turnTotal.

Edited 5 Years Ago by NormR1: n/a

public class DiceGame {
	public static void main(String[] args) throws InterruptedException {
		System.out.println("Let's Play 41!!!");
		System.out.println();
		
		// Initialize
		int rollCount = 0, rollSum = 0;
		Die die1 = new Die(6), die2 = new Die(6);
		
		// Loop
		while (rollSum <= 41) {
			Thread.sleep(2000);
			System.out.println("Rolling Dice");	
			Thread.sleep(2000);
			
			// Roll Dice
			die1.roll();
			die2.roll();
			rollSum += die1.getUpValue() + die2.getUpValue();
			rollCount++;
			
			// Display Roll Values
			if (die1.getUpValue() == die2.getUpValue())
				System.out.println("YOU ROLLED DOUBLE " + die1.getUpValue() + "'S! ");
			else
				System.out.println("You rolled " + die1.getUpValue() + " and " + die2.getUpValue());
			Thread.sleep(2000);
			System.out.println("Your total is: " + rollSum);
			System.out.println("Your roll count is: " + rollCount);
			
			// Game Rules
			if (rollSum == 10 || rollSum == 20 || rollSum == 30 || rollSum == 40 || rollCount == 6) {
				System.out.println("You lose!");
				break;
			}
			else if (rollSum == 41)
				System.out.println("You win!");
			System.out.println();
		}
	}
}

@ztini Does giving the OP code help him learn how to write code? You don't learn much by copy and pasting.
How about explaining what the code does and how you designed it so that the OP can do it by himself next time. Especially the statement he was having a problem with.

How about explaining what the code does and how you designed it so that the OP can do it by himself next time

You explain it if you care so much.

All I see is a bunch of posts wasting the OP's time. Perhaps your teaching style is "blah, blah". I prefer to see code.

You disagree that commenting code will help OPs learn about programming?

Code without comments is like you are trying to show off.
A lot of us care to help students learn programmning.

Edited 5 Years Ago by NormR1: n/a

You disagree that commenting code will help OPs learn about programming?

Don't put words in my mouth. You don't help OP's, you just talk to them...endlessly. Show me the code!

This article has been dead for over six months. Start a new discussion instead.