【问题标题】:Die Simulator nested loop issue模具模拟器嵌套循环问题
【发布时间】: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骰子有几面???

标签: java dice


【解决方案1】:

我认为您要做的是将这些骰子掷出一定的次数,并跟踪达到每个总数的频率,以便您可以在最后打印它们。试试这个:

int[] sumArray = new int[maxRoll];
for (int i = 0; i < numRol; i++) {
    int sum = 0;
    for (Die d : diceArray) {
        int roll = d.roll();
        sum += roll;
        System.out.print("\t" + roll);
    }
    System.out.println("\t:\t" + sum);
    sumArray[sum-1]++;
}
for (int i  = 0; i < sumArray.length; i++){
    System.out.printf("%d: %d Rolls\n", i+1, sumArray[i]);
}

你可以看到它在工作here。你最基本的错误是:

  1. 在开始计算总和之前声明总和数组。
  2. 在内部循环中,迭代您的骰子,一次一个,滚动,相加,然后打印。
  3. 在掷完骰子后打印您的总和并增加您的计数。

如果你用这个算法掷两个六面骰子 10 次,你会得到:

Results
    4   3   :   7
    5   5   :   10
    2   2   :   4
    6   5   :   11
    1   1   :   2
    6   5   :   11
    6   5   :   11
    1   2   :   3
    2   1   :   3
    3   5   :   8
1: 0 Rolls
2: 1 Rolls
3: 2 Rolls
4: 1 Rolls
5: 0 Rolls
6: 0 Rolls
7: 1 Rolls
8: 1 Rolls
9: 0 Rolls
10: 1 Rolls
11: 3 Rolls
12: 0 Rolls

【讨论】:

  • 感谢您的帮助!我真的很感激。
  • @user3023253 - 这有帮助吗?更重要的是 - 你知道哪里出错了吗?
  • 确实如此。虽然如果你不介意的话,我确实有一个问题。为什么在内部循环中使用增强的 for 循环更有价值?
  • 我喜欢它有两个原因:语法更具交流性,并且不可能误判数组的大小并从数组的末尾走到 ArrayIndexOutOfBoundsException。否则,基本上是等价的。
  • @Frightlin 请考虑接受对您有帮助的答案。否则下次人们会避免回答你的问题
【解决方案2】:

你有这样一行:

diceArray[i].roll();

问题是 diceArray 仅与您拥有的骰子数量一样大。但是您正在尝试将它与卷数索引一起使用。这可能会导致 ArrayOutOfBoundsException。

【讨论】:

  • 那我该如何滚动呢?
  • 外环 = 卷数。内循环 = 骰子数。或相反亦然。只在内环内执行掷骰和求和。
  • 外循环是掷骰数,所以如果我把 diceArray[i].roll();并将 i 更改为 j?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 2016-06-23
相关资源
最近更新 更多