In my program, I'm trying to call the throwDice
method in a different class.
public class SimpleDice {
private int diceCount;
public SimpleDice(int the_diceCount){
diceCount = the_diceCount;
}
public int tossDie(){
return (1 + (int)(Math.random()*6));
}
public int throwDice(int diceCount){
int score = 0;
for(int j = 0; j <= diceCount; j++){
score = (score + tossDie());
}
return score;
}
}
import java.util.*;
public class DiceTester {
public static void main(String[] args){
int diceCount;
int diceScore;
SimpleDice d = new SimpleDice(diceCount);
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of dice.");
diceCount = scan.nextInt();
System.out.println("Enter target value.");
diceScore = scan.nextInt();
int scoreCount = 0;
for(int i = 0; i < 100000; i++){
d.throwDice();
if(d.throwDice() == diceScore){
scoreCount += 1;
}
}
System.out.println("Your result is: " + (scoreCount/100000));
}
}
When I compile it, an error pops up for the d.throwdice()
and says it can't be applied. It says it needs an int and there are no arguments. But I called an int diceCount
in the throwDice
method, so I don't know what's wrong.
throwDice
throws the dicediceCount + 1
times because thefor
loop's condition isj <= diceCount
. It will throw the dice forj
from0
throughdiceCount
. – rgettman