【问题标题】:Dice Program in JavaJava中的骰子程序
【发布时间】:2014-09-23 06:02:45
【问题描述】:

我正在制作一个骰子模拟器,我的任务是一次掷 4 个骰子,将骰子的每个面相加,然后根据需要多次重复该过程。然后我必须记录每次掷骰(4 个骰子相加)发生了多少次,并做一个表格。

我基本上在苦苦挣扎的是如何循环我的代码,以便它可以根据需要多次重复滚动,并跟踪每个滚动的数值。到目前为止,这是我的代码:

这是我的 Die 程序:

import java.util.Random;

public class Die
{
   private Random generator;
   private int sides;
   public Die (int s)
   {
      sides = s;
      generator = new Random();
   }

   public int cast()
   {
      return 1 + generator.nextInt(sides);
   }
}

这是我的模具模拟器程序:

import java.util.Scanner;
import java.util.Random;

public class DieSimulator
{
   public static void main(String[] args)
   {
      Die d1 = new Die(6);
      Die d2 = new Die(6);
      Die d3 = new Die(6);
      Die d4 = new Die(6);
      final int TRIES = 1;
      for (int i = 1; i <= TRIES; TRIES +)
      {
         int n1 = d1.cast();
         //System.out.print(n1 + "");
         int n2 = d2.cast();
         //System.out.print(n2 + "");
         int n3 = d3.cast();
         //System.out.print(n3 + "");
         int n4 = d4.cast();
         System.out.print(n4 + n3 + n2 + n1);
         n = n4 + n3 + n2 + n1

      }
      System.out.println();
   }
}

模具模拟器程序的评论部分只是我试图弄清楚如何发布滚动的数值。

任何帮助将不胜感激

【问题讨论】:

  • 你的程序的终止条件是什么?什么时候应该停止?您现在也没有跟踪每一卷。
  • 你能解释一下你的意思,我只是想弄清楚如何发布卷的数值。?
  • 这还能编译吗? :for (int i = 1; i &lt;= TRIES; TRIES +)
  • 只需创建一个列表,循环几次,然后将掷骰子的每个结果添加到列表中
  • for (int i = 1; i &lt;= TRIES; i++) ...

标签: java loops random dice


【解决方案1】:

我真的很喜欢你的程序的结构,但我认为如果你不打扰另一个类,它会简单得多。

跟踪每个单独滚动的最简单方法是:

**int [][] results = new int[nDices][nRolls];**

您可以通过以下方式轻松遍历每个投掷:

Random r = new Random();
int currSum=0;
int oneRollSum=0;
for(int i=0; i<nRolls;i++){
    for(int j=0; j<nDices; j++){
       results[j][i]= r.nextInt(6)+1;
       oneRollSum=oneRollSum+results[j][i];
       currSum=results[j][i]+currSum;
    }
    oneRollSum=0;

}

【讨论】:

    【解决方案2】:

    您可以创建一个处理四个骰子的新类:

    public class Dice {
        private Die[] dice;
        public Dice(int noOfDice, int maxValueOfDice) {
            dice = new Die[noOfDice];
            for( int i = 0; i < dice.length; ++i){
                dice[i] = new Die(maxValueOfDice);
            }
        }
        public int cast(){
            int sum = 0;
            for( Die d : dice){
                sum += d.cast();
            }
            return sum;
        }
    }
    

    使用地图或数组可以跟踪滚动的值:

    import java.util.HashMap;
    import java.util.Map;
    
    public class Statistics {
        static int TRIES = 1000;
        public static void main(String[] args)
        {
            Dice dice = new Dice(4, 6);
            Map<Integer,Integer> map = new HashMap<>();
            // roll TRIES times
            for (int i = 1; i <= TRIES; ++i){
                int value = dice.cast();
                if( map.containsKey(value)){
                    map.put(value, map.get(value)+1);
                } else {
                    map.put(value,1);
                }
            }
            // print all results:
            for( Integer key : map.keySet()){
                System.out.println("Value " + key + " rolled " + map.get(key) + " times");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-21
      • 2014-08-25
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 2013-10-29
      相关资源
      最近更新 更多