Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that asks the user for the following: 1) How many dice to roll (up to 6 dice) 2) How many sides per

Write a program that asks the user for the following:

1) How many dice to roll (up to 6 dice) 2) How many sides per die (all dice can be the same number of sides, maximum 10) 3) How many times to repeat rolling the dice (maximum 10,000,000) // After all information is received, simulate // rolling the dice the specified number of times // and show the tallied results // // When you show your results, show only // the possible range of values based on // the user's input // // Example: given 2 6-sided to roll, show // tallies for the range of 2-12

Below is the code that I have created so far. It allows me to select amount of sides and amount of rolls but not amount of dice.

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package week7;

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

/** * * */ public class Week7 {

public static int roll(int sides) { Random numberOfSides = new Random(); return numberOfSides.nextInt(sides) + 1; } /** * @param args the command line arguments */ public static void main(String[] args) { int tally1 = 0; int tally2 = 0; int tally3 = 0; int tally4 = 0; int tally5 = 0; int tally6 = 0; int tally7 = 0; int tally8 = 0; int tally9 = 0; int tally10 = 0; int result; Scanner input = new Scanner(System.in); System.out.print("Enter number of sides on the dice: "); System.out.print("Do not choose more than 10 sides! "); int userInput = input.nextInt(); System.out.print("Enter number of rolls: "); System.out.print("10,000,000 is the most you can do! "); int rolls = input.nextInt(); System.out.print("Enter number of dice: "); System.out.print("6 die is the most you can do! "); int AmountOfDice = input.nextInt(); for (int c = 0; c < rolls; c++) { result = roll(userInput); // Tally the results design: // If result is 1, add 1 to tally1 // else if result is 2, add 1 to tally2 // etc. switch(result) { case 1: tally1++; break; case 2: tally2++; break; case 3: tally3++; break; case 4: tally4++; break; case 5: tally5++; break; case 6: tally6++; break; case 7: tally7++; break; case 8: tally8++; break; case 9: tally9++; break; case 10: tally10++; break; } } System.out.printf("1 : %d ", tally1); System.out.printf("2 : %d ", tally2); System.out.printf("3 : %d ", tally3); System.out.printf("4 : %d ", tally4); System.out.printf("5 : %d ", tally5); System.out.printf("6 : %d ", tally6); System.out.printf("7 : %d ", tally7); System.out.printf("8 : %d ", tally8); System.out.printf("9 : %d ", tally9); System.out.printf("10: %d ", tally10); int total = tally1 + tally2 + tally3 + tally4 + tally5 + tally6 + tally7 + tally8 + tally9 + tally10; System.out.printf("Total rolls: %d ", total);

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

a valuing of personal and psychological privacy;

Answered: 1 week ago