0

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;
}
}
asked Jun 12, 2017 at 18:48
2
  • what is your problem? Commented Jun 12, 2017 at 18:51
  • i see that you don't initialize the scanner Scanner input = new Scanner(System.in); second you don't call your method amount Commented Jun 12, 2017 at 18:53

4 Answers 4

1

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
answered Jun 12, 2017 at 19:21
Sign up to request clarification or add additional context in comments.

Comments

1

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
answered Jun 12, 2017 at 19:12

4 Comments

I can see where i was missing some things but how would I get the output to look like the sample I showed, how do I get it to count the amount of times for each number rolled?
@Yahya Please see your output and sum the times (attempts ) it's bigger than 10. Read the question and review Your answer.
@michael I see now , you want the sum , just the total of every round
@TarasShpulyar It's the sum my friend :)
0

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));
 }
}
answered Jun 12, 2017 at 19:05

2 Comments

My friend, this answer doesn't solve the OP question. Please edit or delete.
@Yahya Let the OP decide that.
0

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.

answered Jun 12, 2017 at 19:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.