Question:
seeking help with "dice roll" java code?
savedbyone
2011-10-20 17:49:28 UTC
I am having trouble with a code to roll dice 1000 times, its not calculating accrording to the sample output I have and i am not sure how to show the last two lines of my code.

My assignment says:
"Store the sum resulting from each roll of two dice in an array, determine the number of times each 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12 was rolled, and determine the number of the roll on which the last 7 and the number of the roll on which last 11 occurred.

The output should indicate the frequency of each possible sum resulting from a roll, and should indicate the rolls on which the last 7 and the last 11 occurred. Output should go to command line screen."

Could someone look at my code and help me fix this??


import java.util.Random;

public class diceroll {


public static void main(String[] args) {


Random randomNumbers = new Random();

int die1;
int die2;
int[] totals = new int[13];

for (int index = 0; index < totals.length; index++)
totals[index]=0;

for (int roll = 1; roll <= 1000; roll++){
die1 = 1 + randomNumbers.nextInt(6);
die2 = 1 + randomNumbers.nextInt(6);
totals[die1 + die2]++;
}
System.out.printf(" %12s%18s\n",
"Sum of Spots on Faces"," Frequency of Sum");

for (int k = 2; k < totals.length; k++){

System.out.printf(" %12d%22d\n", k, totals[k]);

}

System.out.println();
System.out.println("Seven last occured on roll");
System.out.println("Eleven last occured on roll");
}
}
Three answers:
deonejuan
2011-10-20 18:26:46 UTC
import java.util.Arrays;

import java.util.Random;





public class DieStatLast711 {



public static void main( String[] args ) {

int[] combos = new int[ 12 +1] ;

int indexOseben = 0; // seben is german 7

int indexOelf = 0; // elf is german 11

// null is German Zero, pronounced nool for them, like hulll for us



int[] dieRolls = new int[1000];

Random r = new Random();



for (int i = 0; i < dieRolls.length; i++) {

int d1 = r.nextInt(6)+1; // kill the zeros, +1

int d2 = r.nextInt(6)+1;

// System.out.printf("%d,%d%n",d1,d2);

dieRolls[i] = d1 + d2;

combos[ dieRolls[i] ]++;

indexOseben = ( dieRolls[ i ] == 7) ? i : indexOseben;

indexOelf = ( dieRolls[ i ] == 11 ) ? i : indexOelf;

}

System.out.println(Arrays.toString( combos ));

System.out.printf("Last Seven: %d%n", indexOseben );

System.out.printf("Last Eleven: %d%n", indexOelf );

}

}
McFate
2011-10-20 17:54:29 UTC
If you want to track "the last roll when a 7 occurred," then you have to do that in the loop where you are doing the rolls. Above the loop with the other variable declarations, put something like:



int last7rollNo = 0;



Then in your loop after calculating the die1 and die2 values, update that roll number if necessary:



if ( (die1 + die2) == 7 ) last7rollNo = roll;



Then after your loop completes, last7rollNo will be the number you want. (Similarly for 11.)
anonymous
2016-12-08 13:21:53 UTC
positioned up an illustration: "i desire some ****** ninjas for a expert conceal and bypass seek for group" in the time of how, you will desire to call your group some concern hella loopy ideas-blowing like: Rocketbooster Ninja Wombats or somethin hella expert like that. additionally upload explosions on the poster. all human beings likes explosions. and dinosaurs.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...