java - Nested Loops and Rolling Dice -
i assigned project in need make program, making use of nested loops, takes user input on how many sides pair of dice has , how many times roll it. program should calculate probability of rolling number combinations. program simple because gives of code, need put in correct order. i've been working @ foir few days haven't seemed able make work. here's have code:
/** * program roll dice selected amount of sides selected amount of times. * * timothy pierce * 1/5/2016 * */ import java.util.scanner; import java.util.random; class diceprobability { public static void main (string[] args) { //initalizing variables int numrolls = 0; int numsides = 0; int match = 0; int sum = 0; //accepting user input scanner in = new scanner(system.in); system.out.print("please enter number of rolls: "); numrolls = in.nextint(); system.out.print("please enter number of sides on dice: "); numsides = in.nextint(); system.out.print("\n sum of dice \t probability \n"); //begin loops for(int roll = 0; roll < numrolls; roll++) { for(sum = 2; sum <= (numsides*2); sum++) { int die1 = 0; int die2 = 0; random randnum = new random(); die1 = randnum.nextint(numsides)+1; die2 = randnum.nextint(numsides)+1; if(die1 + die2 == sum) { match = 0; match++; } double probability = 0.0; probability = (double)match / numrolls * 100; system.out.println(" " + sum + "\t\t" + probability); } } }//end method }//end class all of code there, not in right order. format of output should be: line asking number of rolls, line asking number of sides, line header has 1 column sum of dice , probability, , loops should "roll dice" , predict probability each number. ie have 2 columns: 1 sum, probability. should go down in order 2,3,4,5,6 etc.with corresponding probability in next column directly beside it. appreciated!
maybe there wrong wiht code, not order.so try write one, try it.
/** * program roll dice selected amount of sides selected amount of times. * * fergus wang * 1/6/2016 * */ import java.util.scanner; import java.util.random; class diceprobability { public static void main (string[] args) { //initalizing variables int numrolls = 0; int numsides = 0; int match = 0; int sum = 0; //accepting user input scanner in = new scanner(system.in); system.out.print("please enter number of rolls: "); numrolls = in.nextint(); system.out.print("please enter number of sides on dice: "); numsides = in.nextint(); system.out.print("\n sum of dice \t probability \n"); // list save match times. int[] matchlist = new int[numsides*2+1]; // creat random tool. random randnum = new random(); // begin loops for(int roll = 0; roll < numrolls; roll++) { int die1 = 0; int die2 = 0; die1 = randnum.nextint(numsides)+1; die2 = randnum.nextint(numsides)+1; int tempsum = die1 + die2; // check sum value. for(sum = 2; sum <= (numsides*2); sum++) { if(tempsum == sum) { matchlist[sum-2]++; } } } for(int index = 0; index<numsides*2-1; index++){ double probability = 0.0; probability = (double)matchlist[index] / numrolls * 100; system.out.println(" " + (index+2) + "\t\t" + probability); } }//end method }//end class
Comments
Post a Comment