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

Thread: Sum not returning correctly

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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:
    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.


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default 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

    }

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Sum not returning correctly

    Thanks man I can't believe I didn't think of that.

Similar Threads

  1. sum of digits
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 02:40 PM
  2. code not returning sum
    By hmcka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2012, 09:17 AM
  3. Sum of intervals
    By cisneros778 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 21st, 2012, 04:08 PM
  4. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM
  5. Need help getting things to loop correctly
    By egruna2 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 9th, 2010, 04:53 PM