michael
michael

Reputation: 117

Dice roll array and method calling

I am working on this question and I can not figure out how to get an array to keep track of the rolls in main and how to loop call the method that is doing the rolls. (Please help I am working on this by myself no teacher.)

The Problem:

Write a program that simulates rolling two dice. Prompt the user to enter the number of dice rolls. Use a loop to repeatedly call a method that simulates a dice roll and returns the total of the two dice to main. Keep track of the rolls in an array in main, and end the program by showing the results of the rolls.

Sample Output:

How many times should I roll the dice? 100
Results for 100 dice rolls 
2 was rolled 4 times
3 was rolled 2 times
4 was rolled 1 times
5 was rolled 8 times
6 was rolled 15 times
7 was rolled 16 times
8 was rolled 17 times
9 was rolled 18 times
10 was rolled 10 times
11 was rolled 6 times
12 was rolled 3 times

My code so far:

import java.util.Scanner; 
public class TestingCenter {
    private static Scanner input;   
    public static void main(String[] args){ 

        System.out.println("How many times should I roll the dice? ");
        int answer = input.nextInt();
        for (int x = 0; x < answer; x++) {  


        }
    }


    public static int amount(int x){
            int die1;   
            int die2;   
            int roll;  
            die1 = (int)(Math.random()*6) + 1;
            die2 = (int)(Math.random()*6) + 1;
            roll = die1 + die2;
            return roll;

}
}

Upvotes: 0

Views: 7573

Answers (4)

Yahya
Yahya

Reputation: 14062

You may try something like this (Explanation in Comments):

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner; 

public class TestingCenter {

    // map to hold the results
    static Map<Integer, Integer> result = new HashMap<>();

    public static void main(String[] args){ 

        // fill the map with numbers from 1 to 12 like the die
        // each number has a corresponding value, initially 0 
        for(int i=1; i<=12; i++){
            result.put(i, 0);
        }

        // initialize the Scanner
        Scanner input = new Scanner(System.in);

        System.out.print("How many times should I roll the dice? ");
        int answer = input.nextInt();

        // repeat the rolling 
        for (int x = 0; x < answer; x++) { 
            rollDice();
        }

        input.close();

        // final result 
        System.out.println("Results for " + answer + " dice rolls:");
        for(Integer die : result.keySet()){
            System.out.println(die + " was rolled " + result.get(die) + " times");
        }
    }

    // this method returns the roll result
    public static void rollDice(){
         int die1, die2, roll;
         die1 = (int)(Math.random()*6) + 1;
         die2 = (int)(Math.random()*6) + 1;
         roll = die1 + die2;
         // increment the current value and update the map result
         result.put(roll, result.get(roll)+1); 
    }
}

Test

How many times should I roll the dice? 100
Results for 100 dice rolls:
1 was rolled 0 times
2 was rolled 5 times
3 was rolled 1 times
4 was rolled 8 times
5 was rolled 17 times
6 was rolled 12 times
7 was rolled 22 times
8 was rolled 8 times
9 was rolled 10 times
10 was rolled 7 times
11 was rolled 9 times
12 was rolled 1 times

Upvotes: 1

Taras Shpulyar
Taras Shpulyar

Reputation: 134

This is implementation with int[] in main method :

import java.util.Random;
import java.util.Scanner;

public class TestingCenter {

private static final Random random = new Random();

public static void main(final String[] args) {

    System.out.println("How many times should I roll the dice? ");
    int answer = 0;
    try (Scanner scanner = new Scanner(System.in)) {
        answer = scanner.nextInt();
    }
    final int[] results = new int[11];
    for (int x = 0; x < answer; x++) {
        results[amount() - 2]++;
    }
    System.out.println(String.format("Results for %s dice rolls ", answer));
    for (int i = 0; i < 11; i++) {
        System.out.println(String.format("%s was rolled %s times", i + 2, results[i]));
    }
}

    public static int amount() {
        return random.nextInt(6) + random.nextInt(6) + 2;

    }
}

Tested output:

How many times should I roll the dice? 
100
Results for 100 dice rolls 
2 was rolled 1 times
3 was rolled 8 times
4 was rolled 10 times
5 was rolled 12 times
6 was rolled 14 times
7 was rolled 18 times
8 was rolled 13 times
9 was rolled 12 times
10 was rolled 6 times
11 was rolled 4 times
12 was rolled 2 times

Upvotes: 1

cocosushi
cocosushi

Reputation: 144

What you could do is create a HashMap, with a key containing the result of the roll, and the second value being the number of times this result was rolled out. It would give something like this :

HashMap<Integer, Integer> results = new HashMap<Integer, Integer>();

for (int x = 0; x < answer; x++) {  
     int roll = amount(x);
     if(results.get(roll) == null) {
          results.put(roll, 1);
     } else {
          results.put(roll, results.get(roll) +1);
     }
}

Then, when printing, you just have to do a for loop (from 1 to 12) and then get the value associated to every number looped.

Upvotes: 0

Adam
Adam

Reputation: 2440

import java.util.Arraylist and simply add each value to the List as your looping answer number of times.

public static void main(String[] args)
{ 
    input = new Scanner(System.in);
    System.out.println("How many times should I roll the dice? ");
    int answer = input.nextInt();
    List<Integer> list = new ArrayList<Integer>();
    for (int x = 0; x < answer; x++)
    {  
        list.add(amount(x));
    }
}

Upvotes: 0

Related Questions