Help With Histogram Dice Program

Need help with this Histogram Dice Program Please

Page 1 of 1

0 Replies - 959 Views - Last Post: 18 November 2009 - 12:10 PM Rate Topic: -----

#1 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

Help With Histogram Dice Program

Post icon  Posted 18 November 2009 - 12:10 PM

Hey guys, I need some assistance with this program that I'm working on. The error is that when you run the program and you enter the number of rolls you would like, and then hit p to print it doesn't pop up any *'s for the result. However if you run an additional role and then print the results it will show the results using *'s. I dont get why it wont show the result the first role you simulate but it will the second.

**DRIVER**
import java.util.Scanner;

public class DiceDriver {

public static void main(String[] args)
{

  char choice; //user's choice of what he/shewants to do
 boolean done = false; //flag that tells whether user wants to quit

Scanner keyboard= new Scanner(System.in);
Dice DiceRoll = new Dice();
System.out.println("Welcome to my dice throwing simulation");

do
{
System.out.println("Options: (n)ew simulation, (a)dditional rolls," +
				   " (p)rint report, (q)uit");
System.out.print("Enter n, a, p, or q ==> ");
choice = keyboard.next().charAt(0);

switch (choice)
{
case 'n': case 'N':
DiceRoll.newSimulation();
break;
case 'a' : case 'A':
DiceRoll.additionalRolls();
break;
case 'p': case 'P':
DiceRoll.printReport();
break;
case 'q': case 'Q':
done = true;
break;
default:
System.out.println("Invalid selection.");
}// end switch
}while (!done);
}
}// end main



**CLASS**
I believe the error is coming from here, but I'm not for sure.
for(i=0; i<numofAsterisks; i++)



**Class**
import java.util.Scanner;
public class Dice
{
Scanner keyboard= new Scanner(System.in);

//member data
int sum;
int i;
int aRolls;
final int numofRolls = 13;
int rolls;
int [] frequency = new int [numofRolls + 1];

//New Simulation
public void newSimulation()
{
int n = 12;
for (int i=2; i<n; i++)
{
frequency[i] = 0;
}
System.out.print("How many dice rolls would you like to simulate?");
rolls = keyboard.nextInt();
}

//Roll the dice additional times
public void additionalRolls()
{



System.out.print("How many additional dice rolls would you like to simulate?");
aRolls = keyboard.nextInt();
rolls += aRolls;
results(aRolls);
}

//Get Results
public void results(int totalRolls)
{
//int sum;
//int i;

for(i=0; i<totalRolls; i++)
{
sum = (int) (Math.random()*6) +1
+ (int) (Math.random()*6) +1;
frequency[sum]++;
}
}

//Print Histogram
public void printReport()
{
String rollSpace = " ";
float tRolls;
int numofAsterisks;
double onePercent = rolls / 100;
int sum;
int i;
//Histogram
System.out.println("DICE ROLLING SIMULATION RESULTS");
System.out.println("Each '*' represents 1% of the total number of dice rolls.");
System.out.println("Total number of dice rolls = " +rolls);

for(sum=2; sum<=12; sum++)
{
rollSpace = "";
//tRolls = (float) frequency[sum]/rolls;
//numofAsterisks = Math.round(tRolls * 100);
numofAsterisks = (int) (frequency[sum] / onePercent);
System.out.println(sum + ": ");

//if(sum<10)
//{
//rollSpace += "";
//}
//rollSpace += sum+":";
for(i=0; i<numofAsterisks; i++)
{

//rollSpace += "*";
System.out.print("*");
}
}

}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1