Getting user input to define the array size

Dice Rolls- timing it and rate per second...

Page 1 of 1

3 Replies - 3527 Views - Last Post: 07 May 2008 - 07:41 AM Rate Topic: -----

#1 mittens   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-September 07

Getting user input to define the array size

Post icon  Posted 06 May 2008 - 11:34 PM

Hi all,

I have this problem with my latest assignment(Dice Roll). I am asked to generate a user defined amount of rolls of a dice, then display :

1. the total number of dice rolls performed,
2. the time taken in milliseconds,
3.the rate of dice rolls (i.e., rolls per second) and
4.the number of times each result was achieved both as a number and as a percentage.

My (very incomplete) code compiles and runs fine, but have NO idea how to get the user input to be the number of times the dice is to be rolled.
We are not supposed to store the random numbers in an array.

I have also never seen or heard of a timer... can anyone post an example of what it is and how its done?

Thanx in advance

import B102.*;
import java.util.Random;

class dr1
{
   public static void getRandom(int SIDES, int index, int num[])
   {
	 Random rand = new Random();
	 int newNum = 0;
	
	   for(index=0; index<num.length; index++)
		{
		  newNum = rand.nextInt();

		  newNum = Math.abs(newNum);

		  newNum = (newNum % SIDES)+1;

		  num[index]=newNum;
		 }

		return;
   }

public static int countRoll(int index, int SIDES, int num[], int rolls, int count)
   {
	 for(index=0; index<num.length; index++)
	 { 
	
	   if(num[index]==rolls)
	   count++;
	 }
	 return(count);
   }

public static float calcPercent(int count, int NUM_ROLLS)
   {
	 float percent;

	   percent=((float)count*100/NUM_ROLLS);

	   return(percent);

   }

public static void printResults(int count, int rolls, int NUM_ROLLS, float percent)
   {
		
	 {
	 if(count>0)
	   {
		  Screen.out.println("There were " + count + " " + rolls + "'s rolled");
		  Screen.out.println("The percentage of " + rolls + "'s is " + percent + "%");
				
	   }

	 else
	   {
	   Screen.out.println("No "+rolls+"'s rolled");
	   }
	   return;
	 } 
   }

public static void main(String args[])
   {
	 final int SIDES = 6;
	 final int NUM_ROLLS = 10000000;
	 int index = 0;
	 int i = 0;
	 int count = 0;
	 float percent = 0;
	
	   int num[] = new int[NUM_ROLLS];	

	   int rolls[] = {1, 2, 3, 4, 5, 6};
			
		{	
		  getRandom(SIDES, index, num);			
		
	for(i=0; i<rolls.length; i++)	
	{

	   count = countRoll(index, SIDES, num, rolls[i], count);

		percent = calcPercent(count, NUM_ROLLS);

		printResults(count, rolls[i], NUM_ROLLS, percent);
				
	//Re-set count and percent for next stage of for loop
			count = 0;
			percent = 0;
				
	}
		  }	
	return;
   }
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Getting user input to define the array size

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Getting user input to define the array size

Posted 07 May 2008 - 12:39 AM

You should do that

import B102.*;
import java.util.Random;

class dr1
{
   // define it once
   static Random rand = new Random();
   // To read user input
   static Scanner scan = new Scanner(System.in);

   public static void getRandom(int SIDES, int index, int num[])
   {
////	 Random rand = new Random();
	 int newNum = 0;
	
	   for(index=0; index<num.length; index++)



Do not ot define a new Random object every time you call getRandom()

System.out.print("Enter a number: ");
int nb = scan.nextInt();

will give you the number entered by the user.

System.currentTimesMillis() return in a long the time in milliseconds
so:
long from = System.currentTimesMilllis();
... code
... code
long now = System.currentTimeMillis();

long delta = now - from;

delta contains milliseconds since from.

Happy coding
Was This Post Helpful? 0
  • +
  • -

#3 mittens   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-September 07

Re: Getting user input to define the array size

Posted 07 May 2008 - 04:22 AM

Thanks for that pbl, but can you please show me an example of how the timer thing works?

I (think) have done as you suggested, but its not working.
Did I do it right??

import B102.*;
import java.util.Random;
import java.util.Date;

class dr2
{
   static Random rand = new Random();

	  Screen.out.print("How many times do you want the dice to be rolled?");
	  int num = Keybd.in.nextInt();

 System.currentTimesMillis(int long);   //return the time in milliseconds

	{
	long from = System.currentTimesMilllis();
   

void getRandom(int SIDES, int index, int num[])
   {

		int newNum = 0;
	
		for(index=0; index<num.length; index++)
		 {
		  newNum = rand.nextInt();

		  newNum = Math.abs(newNum);

		  newNum = (newNum % SIDES)+1;

		 num[index]=newNum;
		 }

		 return;
   }

	  long now = System.currentTimeMillis();

	  long delta = now - from;
  }

public static int countRoll(int index, int SIDES, int num[], int rolls, int count)
  {
	for(index=0; index<num.length; index++)
		{
	
		if(num[index]==rolls)
	count++;
		}
	   return(count);
   }

public static float calcPercent(int count, int NUM_ROLLS)
	 {
	 float percent;

	 percent=((float)count*100/NUM_ROLLS);

	 return(percent);
	 }

public static void printResults(int count, int rolls, int NUM_ROLLS, float percent)
	{
		
		{
	if(count>0)
	{
						
	Screen.out.println("There was "+count+" "+rolls+"'s rolled");
				Screen.out.println("The  percentage of "+rolls+"'s is "+percent+"%");
				
	}

	else
	{
		  Screen.out.println("No "+rolls+"'s rolled");
	}
return;
	 } 

   }

public static void main(String args[])
 {
	final int SIDES = 6;
	final int NUM_ROLLS = 10000000;
	int index = 0;
	int i = 0;
	int count = 0;
	float percent = 0;

	//Declare empty array for Random numbers
   int num[] = new int[NUM_ROLLS];	

	//Declare array for sides of dice to compare random numbers to	
	int rolls[] = {1, 2, 3, 4, 5, 6};
		
		
	{
	//Pass SIDES and index by value to getRandom method
	//Pass empty array for random numbers by reference
	getRandom(SIDES, index, num);			
		
	for(i=0; i<rolls.length; i++)	
	{

	count = countRoll(index, SIDES, num, rolls[i], count);

	percent = calcPercent(count, NUM_ROLLS);

	printResults(count, rolls[i], NUM_ROLLS, percent);
				
	//Re-set count and percent for next stage of for loop
	count = 0;
	percent = 0;
	}
  }	
  return;
 }
}



I am getting 4 errors..

1. line 14 : .class expected (currenttimeMilliseconds( int long);

2. line 14: ' )' expected (int long);


3. line 19: illegal start of expression (public static countRoll)

4. line 43 : illegal start of expression public static int countRoll(........
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Getting user input to define the array size

Posted 07 May 2008 - 07:41 AM

View Postmittens, on 7 May, 2008 - 04:22 AM, said:

Thanks for that pbl, but can you please show me an example of how the timer thing works?

I (think) have done as you suggested, but its not working.
Did I do it right??


Not exactly as I suggest :)

You had to put 3 things at different places

import B102.*;
import java.util.*;
import java.util.Date;

class dr2
{
   // variables declaration
   static Random rand = new Random();
   static Scanner scan = new Scanner(System.in);

void getRandom(int SIDES, int index, int num[])
   {

		int newNum = 0;
	
		for(index=0; index<num.length; index++)
		 {
		  newNum = rand.nextInt();

		  newNum = Math.abs(newNum);

		  newNum = (newNum % SIDES)+1;

		 num[index]=newNum;
		 }

		 return;
   }

	  long now = System.currentTimeMillis();

	  long delta = now - from;
  }

public static int countRoll(int index, int SIDES, int num[], int rolls, int count)
  {
	for(index=0; index<num.length; index++)
		{
	
		if(num[index]==rolls)
	count++;
		}
	   return(count);
   }

public static float calcPercent(int count, int NUM_ROLLS)
	 {
	 float percent;

	 percent=((float)count*100/NUM_ROLLS);

	 return(percent);
	 }

public static void printResults(int count, int rolls, int NUM_ROLLS, float percent)
	{
		
		{
	if(count>0)
	{
						
	Screen.out.println("There was "+count+" "+rolls+"'s rolled");
				Screen.out.println("The  percentage of "+rolls+"'s is "+percent+"%");
				
	}

	else
	{
		  Screen.out.println("No "+rolls+"'s rolled");
	}
return;
	 } 

   }

public static void main(String args[])
 {
	final int SIDES = 6;
///	final int NUM_ROLLS = 10000000;
				int num_rools;
	int index = 0;
	int i = 0;
	int count = 0;
	float percent = 0;

	long from = System.currentTimesMilllis();
	 System.out.print("How many times do you want the dice to be rolled?");
	 int num_rools = Keybd.in.nextInt();
   


	//Declare empty array for Random numbers
				int num[] = new int[num_rools];	

	//Declare array for sides of dice to compare random numbers to	
	int rolls[] = {1, 2, 3, 4, 5, 6};
		
		
	{
	//Pass SIDES and index by value to getRandom method
	//Pass empty array for random numbers by reference
	getRandom(SIDES, index, num);			
		
	for(i=0; i<rolls.length; i++)	
	{

	count = countRoll(index, SIDES, num, rolls[i], count);

	percent = calcPercent(count, NUM_ROLLS);

	printResults(count, rolls[i], NUM_ROLLS, percent);
				
	//Re-set count and percent for next stage of for loop
	count = 0;
	percent = 0;
	}
				long now = System.currentTimeMillis();
				long delta  = now - from;
				System.out.println("It took " + delta + "ms");
  }	
  return;
 }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1