为什么我输入骰子数 “1”, 摇出来的总是零,然后就死机了。牛人们帮我看看吧。
多谢了!
import acm.util.*;
import acm.program.*;
public class MyDice extends ConsoleProgram {
private static final int NUM_SIDES = 6;
public void run (){
int numDice = readInt("Number of dices: ");
int maxRoll = numDice * NUM_SIDES;
int numRolls = 0;
while (true) {
int roll = rollDice (numDice);
numRolls++;
if (roll == maxRoll) break;
println ("Rolled = " + roll);
} print ("Rolled " + maxRoll +"after "+ numRolls + " roles.");
}
private int rollDice(int numDice) {
int total = 0;
for (int i=1; i < numDice; i++) {
total = total + rgen.nextInt (1, NUM_SIDES);
}
return total;
}
/* Private instance variables */
private RandomGenerator rgen = RandomGenerator.getInstance();
}