I have the code done for the rest of my program but I can not get the asterisks in my histogram to print can you help??

Thanks In Advance!!

Here is a copy of my code (driver and class).

 public class diceSimulation
{
    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

      diceSimulation2 diceSimulation = new diceSimulation2();
      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 = Input.readChar();

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

This is the Class

public class diceSimulation2
{
  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 = Input.readInt();
 }

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

    int aRolls;


System.out.print("How many additional dice rolls would you like to simulate?");
    aRolls = Input.readInt();
    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;

    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("*");

      }

   }

}
}

Recommended Answers

All 5 Replies

Use code tags.
Tell exactly what's going on and what's going wrong.

I'm not going to read through a hundred lines of malformated code to look for a typo or a missing statement, point me to the correct 10 lines or so and I may take a look.

ok to my understanding the asterisks are suppossed to print during this for loop and I am not getting any asterisks can anyone help me??

for(i=0; i<numofAsterisks ; i++)
{

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

Check out the value of numofAsterisks.
Most likely it's 0...

ok I set numofAsterisks to = 0.
and this is the output I am getting:
(which is the same as I was getting before)


Welcome to my dice throwing simulation
Options: (n)ew simulation, (a)dditional rolls, (p)rint report, (q)uit
Enter n, a, p, or q ==> n
How many dice rolls would you like to simulate?200
Options: (n)ew simulation, (a)dditional rolls, (p)rint report, (q)uit
Enter n, a, p, or q ==> p
DICE ROLLING SIMULATION RESULTS
Each '*' represents 1% of the total number of dice rolls.
Total number of dice rolls = 200
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Options: (n)ew simulation, (a)dditional rolls, (p)rint report, (q)uit
Enter n, a, p, or q ==>

What I meant was that you should check the value of the variable because it probably is 0 and therefore nothing will get printed...

Either that or your frequencies are all so low that (when divided by 2 in your case) the result is less than 1 so gets rounded to 0 and again nothing is printed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.