Reputation: 13
So I have a dice rolling program that seems to work so far, but the only problem I can't figure out is how to sum the outputs of the rolls. It rolls the dice again while summing the previous roll.
Here is the Die class I started with
public class Die{ //only thing changed was removing implementation of the interface
private int noFaces;
public Die() {
noFaces = 6;
}
public int getNoFaces() { //getter
return noFaces;
}
public Die(int noFaces) { //setter, sets new value according to passed parameter
this.noFaces = noFaces;
}
public int roll() { //generates random number+1 within specified "noFaces" range
return (int) (Math.random() * noFaces + 1);
}
public void printRoll() { //goes to above method roll() and prints the returned value
System.out.println(roll());
}
}
and making one Die into a PairOfDice, which is where I'm having the summation issue
public class PairOfDice {
private Die d1, d2;
public PairOfDice() { //initializes 2 die objects
d1 = new Die();
d2 = new Die();
}
public PairOfDice(int noFaces) { //sets the dice equally
d1 = new Die(noFaces);
d2 = new Die(noFaces);
}
public PairOfDice(int noFaces1, int noFaces2) { //sets dice separately
d1 = new Die(noFaces1);
d2 = new Die(noFaces2);
}
public void printRoll() { //prints rolls
System.out.println("Die 1 returned: " + d1.roll());
System.out.println("Die 2 returned: " + d2.roll());
}
public void printRollSum(){ //print sum of both rolls
System.out.println("Sum of both dice rolls are: " + (d1.roll() + d2.roll()));
}
}
and where I'm getting the output, RollingDice
public class RollingDice {
public static void main(String[] args) {
PairOfDice pairOne = new PairOfDice();
pairOne.printRollSum();
System.out.println();
PairOfDice pairTwo = new PairOfDice(10);
pairTwo.printRoll();
pairTwo.printRollSum();
System.out.println();
PairOfDice pairThree = new PairOfDice(100, 3);
pairThree.printRoll();
pairThree.printRollSum();
}
}
All help on either my program or the comments is appreciated, I'm still learning. Thanks!
Upvotes: 0
Views: 7631
Reputation: 1049
Each of your printRoll() and printRollSum() calls is re-rolling the dice. If you're looking to roll the dice, print each individual die's face value, then print the sum of those two face values, you'll need to store the results of the rolls and then perform the sum computation on those results. For example:
public class PairOfDice {
private Die d1, d2;
int lastRollD1;
int lastRollD2;
public PairOfDice() { //initializes 2 die objects
d1 = new Die();
d2 = new Die();
}
public PairOfDice(int noFaces) { //sets the dice equally
d1 = new Die(noFaces);
d2 = new Die(noFaces);
}
public PairOfDice(int noFaces1, int noFaces2) { //sets dice separately
d1 = new Die(noFaces1);
d2 = new Die(noFaces2);
}
public void roll()
{
lastRollD1 = d1.roll();
lastRollD2 = d2.roll();
}
public void printRoll() { //prints rolls
System.out.println("Die 1 returned: " + lastRollD1);
System.out.println("Die 2 returned: " + lastRollD1);
}
public void printRollSum(){ //print sum of both rolls
System.out.println("Sum of both dice rolls are: " + (lastRollD1 + lastRollD1));
}
}
In this version, your RollingDice would then call:
pairOne.roll();
pairOne.printRoll();
pairOne.printRollSum();
Upvotes: 0
Reputation: 666
It generates new numbers every time printRoll
and printRollSum
are called. You should combine the two methods, or at least store the current value of the roll in a die, and access them through printRollSum
Upvotes: 0
Reputation: 78876
As a solution, you could store the last rolled value in your Die
class, (similar to noFaces
, but call it lastValue
for example), and then change printRollSum() to use getLastValue()
instead of roll()
Upvotes: 0
Reputation: 158459
The method roll()
will generate a new random number each time it is called so when you call printRoll()
and printRollSum()
you will receive new numbers each time.
You need to store the results of roll
in one of your classes and then provide a method to access those results.
Upvotes: 0
Reputation: 961
You are getting the sum of the two dice, but you're rolling the dice each time. printRoll()
rolls and displays the values, then printRollSum()
rolls them again, giving you different values.
Upvotes: 3