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:
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; } }
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?