【发布时间】:2014-01-30 13:34:34
【问题描述】:
import java.util.Scanner;
public class DiceSimulator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many dice do you want to roll: ");
int amountOfDie = input.nextInt();
System.out.println("");
// declare diceArray
Die[] diceArray = new Die[amountOfDie];
// create a new Die for each reference
int maxRoll = 0;
for (int i = 0; i < diceArray.length; i++) {
System.out.print("How many sides on die number " + (i + 1) + "" + ": ");
int numSides = input.nextInt();
diceArray[i] = new Die(numSides);
int minRoll = amountOfDie;
maxRoll += numSides;
}
int minRoll = amountOfDie;
// int[] sumArray = new int[maxRoll + 1];//to store sum
System.out.println("");
System.out.print("How many times do you want to roll: ");
int numRol = input.nextInt();
System.out.println("\nResults");
// ******** right here is where I'm having the issue.
for (int i = 0; i < numRol; i++) {
diceArray[i].roll();
// System.out.println(diceArray[i]);
int sum = 0;
for (int f = 0; f < numRol; f++) {
sum += diceArray[f].roll();
int[] sumArray = new int[maxRoll + 1];
sumArray[sum]++;
System.out.print("\t" + sum);
}
}
// for(Die d: diceArray){
// System.out.println(d);
// }
}
}
查看代码中的注释行:// ******** right here is where I'm having the issue.
for 循环应该输出滚动的总和。
它只是没有吐出正确的价值观。我只是好奇我哪里出错了?程序应该询问用户有多少卷。这将进入第一个 for 循环,第二个将掷骰子多次。
【问题讨论】:
-
你的缩进很糟糕。请修复它。
-
"for 循环应该吐出滚动的总和。" ......它做了什么?另外,What have you tried?
-
@FrankieTheKneeMan ,例如。你想掷多少个骰子:3 1 号骰子有多少面:3 2 号骰子有多少面:2 3 号骰子有多少面:4 你想掷多少次:2 结果 2 4 2 4
-
@SotiriosDelimanolis,抱歉,对 java 来说还是很新。
-
你的第一个问题是你输入了你想掷两个骰子,但你的程序询问了#1、#2和#3骰子有几面???