# Sum not returning correctly

• October 28th, 2013, 01:16 AM
thegorila78
Sum not returning correctly
There's this program I have to write for school:
Craps is a popular dice game played in casinos. Write a program to play a
variation of the game, as follows:
Roll two dice. Each die has six faces representing values 1, 2,…, and 6,
respectively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called
craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is
another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll
the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose.
Otherwise, you win. The program should contain a method:
public static int getDice() {}
The method returns the value of the sum of the two dice.

My code so far is as follows:
Code :

```import java.util.Random;     public class Craps { public static void main(String[] args) { System.out.println("You rolled " + d1() + " + " + d2() + " = " + getDice());     }//end main method   public static int getDice(){ int sum = d1() + d2(); return sum; }//end getDice() public static int d1(){ Random myGenerator = new Random(); int d1 = myGenerator.nextInt(6) + 1; return d1; }//end d1() public static int d2(){ Random myGenerator2 = new Random(); int d2 = myGenerator2.nextInt(6) + 1; return d2; }//end d2()   }//end class```

My sum is completely different to what it should be. I know the problem is that I'm calling the method 2 different times which is giving me a random number for each of the calls but I can't think up a solution to solve it. Any help would be appreciated.
• October 28th, 2013, 02:12 AM
aprabhat
Re: Sum not returning correctly
try this buddy...

public static void main(String[] args)
{
int d1 = Craps.d1();
int d2 = Craps.d2();
System.out.println("You rolled " + d1 + " + " + d2 + " = " + getDice(d1,d2));

}//end main method

public static int getDice(int d1,int d2){
int sum = d1+d2;
return sum;
}//end getDice()
public static int d1(){
Random myGenerator = new Random();
int d1 = myGenerator.nextInt(6) + 1;
return d1;
}//end d1()
public static int d2(){
Random myGenerator2 = new Random();
int d2 = myGenerator2.nextInt(6) + 1;
return d2;
}//end d2

}
• October 28th, 2013, 02:18 AM
thegorila78
Re: Sum not returning correctly
Thanks man I can't believe I didn't think of that.