Results 1 to 4 of 4
  1. #1
    kbc080 is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Slight help on program that rolls dice amount of times and counts rolls of snake eyes

    import java.util.Scanner;
    import java.lang.Math;

    public class dice {

    public static int rollDice()
    {
    int snakeEyes=0;

    int dice1 = (int)(Math.random() * 6) + 1;
    int dice2 = (int)(Math.random() * 6) + 1;
    //System.out.println(dice1 + dice2);
    int total = dice1 + dice2;

    if(total==2){
    snakeEyes++;
    }
    System.out.print(snakeEyes);
    return total;
    }

    public static void main(String [] args)
    {
    int diceRolls;

    Scanner sinput = new Scanner(System.in);

    System.out.print("How many times would you like to roll the two dice? ");
    diceRolls = sinput.nextInt();

    for(int counter=1; counter<=diceRolls; counter++){
    rollDice();

    }
    }

    }

    Having trouble keeping the running total of snakeeyes that appear in the method, I've tried everything I can think of and feel like I'm so close any help would be appreciated.

  2. #2
    Reeling is offline Member
    Join Date
    Jan 2012
    Posts
    49
    Rep Power
    0

    Default Re: Slight help on program that rolls dice amount of times and counts rolls of snake

    1. Use code tags.

    2. You run your rollDice() method but you don't assign a variable to the return that it gives.

  3. #3
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Slight help on program that rolls dice amount of times and counts rolls of snake

    think you should return snakeEyes from the rollDice method, and in the main do
    Java Code:
    int snakeeyes;
    for (....)
    snakeeyes+=rollDice();

  4. #4
    Sayco is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Slight help on program that rolls dice amount of times and counts rolls of snake

    Your snakeEyes variable scope is limited to you rollDice() method, you must declare it outside the method in order to use it outside of rollDice(). Also you can use a System.out.println("" + snakeEyes); to get the count.

Similar Threads

  1. Replies: 2
    Last Post: 10-06-2012, 05:29 AM
  2. program ,deposit of an amount of money in a bank
    By aou in forum Advanced Java
    Replies: 1
    Last Post: 08-03-2012, 10:41 PM
  3. Replies: 5
    Last Post: 03-30-2012, 09:53 PM
  4. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-05-2011, 12:08 AM
  5. Replies: 13
    Last Post: 10-31-2010, 09:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •