# Dice score program

• April 23rd, 2012, 12:59 AM
ksahakian21
Dice score program
This is a small project for my class. Need to write a code that takes in two integers in the main class. One integer is the number of dice to be thrown, and the second is a score. The program needs to throw the specified number of dice 100,000 times then report what percent of the time the score enter appeared. So say 4 was enter for the number of dice to be thrown at once, and 6 was the score entered. The program needs to count the number of times the score of 6 occurs when 4 dice are thrown 100,000 times.

Here's my code:

Code :

public class SimpleDice{

public int tossDie(){
return(1 + (int)(Math.random()*6));
}

public int throwSetDice(){
int score = 0;
for(int j = 0; j <= diceCount; j++){
score = (score + tossDie());
}
return score;
}
}

Code :

import java.util.Scanner;

public class DiceTester{
public static void main(String[] args){

int diceCount;
int desiredScore;

Scanner scan = new Scanner(System.in);
System.out.println("Enter number of Dice:");
diceCount = scan.nextInt();
System.out.println("Enter score to count:");
desiredScore = scan.nextInt();
System.out.println("dice: " + diceCount + " score: " + desiredScore);

SimpleDice d = new SimpleDice();
int scoreCount = 0;

for(int i = 0; i < 100000; i++){
d.throwSetDice();
if(d.throwSetDice() == desiredScore){
scoreCount += 1;
}
}
System.out.println("Result: " + (scoreCount/100000));
}
}

I'm getting one error in my SimpleDice class (9th line) in the for loop reading:
"diceCount cannot be resolved to a variable"

How do I call the amount of dice to be thrown at once in the SimpleDice class?
• April 23rd, 2012, 01:45 AM
ksahakian21
Re: Dice score program
I would like to be able to utilize the SimpleDice class more, but I edited the DiceTester class to do the work of the throwSetDice() method. Now I'm running in to another problem though. When trying to print the result (scoreCount/100000), I keep getting an answer of 0. Yet if I print just the scoreCount int, it shows it does have a value but java is not dividing it by 100000. I tried using int, double, long, and i dont know what else could be used. New code:

Code :

import java.util.Scanner;

public class DiceTester{
public static void main(String[] args){

int diceCount;
int desiredScore;
int diceThrows = 100000;

Scanner scan = new Scanner(System.in);
System.out.println("Enter number of Dice:");
diceCount = scan.nextInt();
System.out.println("Enter score to count:");
desiredScore = scan.nextInt();
System.out.println("dice: " + diceCount + " score: " + desiredScore);

SimpleDice d = new SimpleDice();
int scoreCount = 0;

for(int i = 0; i < diceThrows; i++){
int score = 0;
for(int j = 0; j < diceCount; j++){
score = (score + d.tossDie());
}
if(score == desiredScore){
scoreCount += 1;
}
}

double result = (scoreCount/diceThrows);
System.out.println(result);
System.out.println(scoreCount);
}
}

note that the new code only uses the tossDie() method from the SimpleDice class.
• April 23rd, 2012, 04:14 AM
andreas90
Re: Dice score program
Quote:

Originally Posted by ksahakian21
I would like to be able to utilize the SimpleDice class more, but I edited the DiceTester class to do the work of the throwSetDice() method. Now I'm running in to another problem though. When trying to print the result (scoreCount/100000), I keep getting an answer of 0. Yet if I print just the scoreCount int, it shows it does have a value but java is not dividing it by 100000. I tried using int, double, long, and i dont know what else could be used. New code:

Hello ksahakian21!
Since the scoreCount and diceThrows variables are ints you're using integer division and floating point is not considered. There several ways to make it work. You can just cast the division to double
Code java:

double result = (double) (scoreCount/diceThrows);

• April 23rd, 2012, 12:23 PM
ksahakian21
Re: Dice score program
Thanks I should have realized that.
Does anyone know of a way to get my original code to work? Utilizing the throwSetDice method in the SimpleDice class.
• April 23rd, 2012, 01:44 PM
ksahakian21
Re: Dice score program
Not sure if this is the right way to go about this or not, but I'm trying to make a new method in the SimpleDice class which (might?) allow me to call the diceCount variable from the DiceTester class to the SimpleDice class.

Code :

public class SimpleDice{

public int howMany(int the_count){
return the_count;
}

public int tossDie(){
return(1 + (int)(Math.random()*6));
}

public int throwSetDice(){
int score = 0;
for(int j = 0; j <= c.howMany; j++){
score = (score + tossDie());
}
return score;
}
}

Code :

import java.util.Scanner;

public class DiceTester{
public static void main(String[] args){

int diceCount;
int desiredScore;

Scanner scan = new Scanner(System.in);
System.out.println("Enter number of Dice:");
diceCount = scan.nextInt();
System.out.println("Enter score to count:");
desiredScore = scan.nextInt();
System.out.println("dice: " + diceCount + " score: " + desiredScore);

howMany c = new howMany(diceCount);

SimpleDice d = new SimpleDice();
int scoreCount = 0;

for(int i = 0; i < 100000; i++){
d.throwSetDice();
if(d.throwSetDice() == desiredScore){
scoreCount += 1;
}
}
System.out.println("Result: " + (scoreCount/100000));
}
}

• April 23rd, 2012, 03:54 PM
SupportIsPower
Re: Dice score program
So basically you want to make a class that simulates a dice being rolled?

I think you're overcomplicating the whole thing, it doesn't have to be as complicated as you make it out to be, just think simple.
• April 23rd, 2012, 05:15 PM
andreas90
Re: Dice score program
Quote:

Originally Posted by ksahakian21
Not sure if this is the right way to go about this or not, but I'm trying to make a new method in the SimpleDice class which (might?) allow me to call the diceCount variable from the DiceTester class to the SimpleDice class.

I don't think the last posted code will compile. If I were you , I would modify the throwSetDice() method in your first post to take the expected result. So you have:
Code java:

public int throwSetDice(){
int score = 0;
for(int j = 0; j <= diceCount; j++){
score = (score + tossDie());
}
return score;
}

What if you make throwSetDice() take an int parameter and use it like diceCount?
Then in the test class you can call throwSetDice() with diceCount which the user has entered as a parameter.
Hope I was clear.
• April 23rd, 2012, 05:25 PM
SupportIsPower
Re: Dice score program
Quote:

Originally Posted by andreas90
[highlight=java]
public int throwSetDice(){
int score = 0;
for(int j = 0; j <= diceCount; j++){
score = (score + tossDie());
}
return score;
}

I'm pretty sure the for loop will roll the dice and extra time, so if diceCount was 4, it would roll 5 times because you put less than OR equal to diceCount, it should only be less then. The statement would only be correct if you started at 1.