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

Thread: Calling a toss dice method

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Calling a toss dice method

    I wrote a method to toss dice (in it, I used another method--the rollDice method), and I want to know how to call it in the main method, because I'm completely confused on that section! Any help would be great! Here's the method:
    //Toss dice
       public static int tossDice(int sum){
           Random rand = new Random();
           int tries = 0;
           while (sum != 7 && sum != 11) {
           // Roll the dice method
           int roll1 = rollDice();
           int roll2 = rollDice();
           sum = roll1 + roll2;
           System.out.println(roll1 + " + " + roll2 + " = " + sum + ".");
           tries++;
           }
           System.out.println("You won after " + tries + " tries!");
           System.out.println("You rolled a sum of " +sum+ ".");
           return sum; 
       }

    Thank you!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Calling a toss dice method

    You call rollDice() just fine in the tossDice() method. I don't understand what you're asking about calling rollDice() in or from the main() method. Why would you? Have you written rollDice()? Where is it?

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a toss dice method

    I've written rollDice() already! But that's not the problem. What I've posted is a method I've written on my own. My assignment asks me to write my own method (done that) and call it in the main method. I'm asking how I'd call the method I've written above. What would I write to call it?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Calling a toss dice method

    Something like:
    public static void main (String[] args)
    {
        // define sum, . . .
     
        // . . . and then call the method:
        tossDice( sum );
    }

Similar Threads

  1. Method not calling?
    By NTWolf1220 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 20th, 2013, 11:04 AM
  2. Dice Game Program toString() method help
    By JonSnow in forum Java Theory & Questions
    Replies: 4
    Last Post: January 17th, 2013, 02:59 PM
  3. calling this method
    By antnas in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 01:32 PM
  4. Need to write a public instance method for a simple dice program
    By akira25 in forum Object Oriented Programming
    Replies: 3
    Last Post: April 9th, 2012, 04:46 AM
  5. [SOLVED] method calling
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 01:43 AM

Tags for this Thread