Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Java Dice rolling game

Featured Replies

here is what i am supposed to do:

write an app to simulate the rolling to 2 dice. the app should use an object of class Random one to roll the first die and again to toll the second die. The sum of the two values should then be calculated. each die can show an integer value from 1 to 6 so sum should vary between 2 and 12. 7 being the most frequent sum.

your app should roll the dice 36,000 times. USe a 1 dimenasional array to tally the number of times each possible sum appears. Display in a tabular format. determine weather the totals are resaonable.

here is what i got so far:

*Description:Creates a DiceTRacker object and has it roll it's dine X times.

*Then is prints out the counts and percentages.

*Date: 12/8/05

*/

public class DiceGame

{

public static void main(String args[])

{

DiceTracker dt = new DiceTracker();

System.out.println("Welcome to the Dice Game");

dt.rollDice(36000);

dt.printResults();

dt.printPercents();

System.out.println("Was that fun or what!");

}

}

That is about it. I need help, i am having a brain fart and i can' think. it is done in java in console, no graphics or nuthin.

thanks

Edited by Seven Alive


  • Author

oh sorry here is my dicetracker

public class DiceTracker

{

public void rollDice()

{

}

public void printPercents()

{

}

public void printResults()

{

}

}

i am currently trying to figure out how 2 do it myself, i need help tho

  • Author

i need some help i got it 2 roll the dice but i need to sum up everything

ex.

sum total

2 58542

3 21554

etc.

then i also need to do the percents:

ex.

sum precentage

2 16%

3 27%

need help, here is what i have came up with.

public class DiceGame
{
public static void main(String args[])
{
int diceCount = 1;
int diceTotal = 0;

DiceTracker dt = new DiceTracker();
System.out.println("Welcome to the Dice Game");
dt.rollDice(36000, diceCount);
dt.printResults();
dt.printPercents();
System.out.println("Was that fun or what!");
}
}


public class DiceTracker
{
int dice[] = new int[6];
public void rollDice(int roll, int diceCount)
{
int mRoll = roll;
for (int x = 0; x <= mRoll; x++)
{
dice[0] = (int)(Math.random() * 6 + 1);
dice[1] = (int)(Math.random() * 6 + 1);
}
}

public void printPercents()
{
for (x = )
}

public void printResults()
{


}
}

need help please

  • 4 weeks later...

I did it for you for kicks. I think looking through how I did it might benefit you in ways other than increasing your grade. (When it comes to programming, people have the tendency to copy and paste.)

import java.util.Random;

public class rolldice{
public static int rollnumber;
public static int ROLL_LIMIT = 36000;
public static int[] die1= new int[ROLL_LIMIT];
public static int[] die2= new int[ROLL_LIMIT];

/*This function keeps track of how many rolls
*have occured and returns false if it reaches
*ROLL_LIMIT*/
public static boolean rolltrack(){
++rollnumber;
if(rollnumber == ROLL_LIMIT)
return false;
else
return true;
}
/*Generates a random number for each array
*of die numbers. The numbers that are generated
*aren't very random, probably due to the high speed
*at which the function is continuously called.
*/
public static void rollthedice(){
Random rand = new Random();
die1[rollnumber] = rand.nextInt(6)+1;
die2[rollnumber] = rand.nextInt(6)+1;
}
/*This function calculates the statistic of how much
*each number was rolled for each die, then outputs
*the data as text*/
public static void tell_roll_statistics(){
int[] number_rolls1=new int[6];
int[] number_rolls2=new int[6];
double[] number_percent1=new double[6];
double[] number_percent2=new double[6];
for(int j=0;j<6;j++){
for(int k=0;k<ROLL_LIMIT;k++){
if(die1[k] == (j+1))
++number_rolls1[j];//Holds the amount of rolls for each number
if(die2[k] == (j+1)) //in the array number_rolls_percent1 and 2
++number_rolls2[j];
}
number_percent1[j] = ((double)number_rolls1[j]/ROLL_LIMIT)*100;
number_percent2[j] = ((double)number_rolls2[j]/ROLL_LIMIT)*100;
System.out.println("[Die1] % of number "+(j+1)+": "+number_percent1[j]);
System.out.println("[Die2] % of number "+(j+1)+": "+number_percent2[j]);
}
}

public static void main(String args[]){
rollnumber = 0;
do{
rollthedice();
}while(rolltrack());
tell_roll_statistics();
}
}

  • 4 weeks later...

i must do the same exercise but the only different thing is that the results of the dice (2,3,4,5,6,7,8,9,10,11,12) must be displayed with their frequency ( how many times the sum had appeared )

such as

sum frequency

2

3

4

5

6

7

8

9

10

11

12

where 2 and 12 are indeed the least and 7 the most in frequency

hoping for help

@bartpeek

not to seem harsh, but if you can't figure out how to do that with the above code, you should consider giving up on programming. Though I don't like how the previous person implemented it and how I would do it is create a dice roll method that returns the value 1-6 randomly and call it twice storing to ints, adding them together and using that value to increment a position in a pre-intialized array. Then run a for loop to print out something along the line of

System.out.println("The value " + i + " was rolled " + tally + " times.");

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.