【问题标题】:Random dice roll with 6 counters using arrays使用数组随机掷骰子,有 6 个计数器
【发布时间】:2021-02-13 07:04:12
【问题描述】:

这个练习有点挑战性。使用 Javascript 控制台的初学者编码器。我不知道如何在自己的行中打印每个数字。

问题:

使用变量、数组和常量来展示随机掷骰子的方式。 定义一个循环调用随机函数 10000 次。使用 6 个计数器来显示每个数字出现的时间。显示骰子编号和它出现的次数,然后是一个按比例显示的直方图,每 40 次出现 1 颗星。

输出应如下所示:

1: (1587) 11111111111111111111111
2: (1542) 222222222222222
3: (1561) 333333333333333333333333
4: (1590) 44444444444444444444444
5: (1523) 555555555555555555
6: (1509) 66666666666666666666

这是我目前所拥有的。

var NUM_ROLLS = 10000;

function start(){
    var rolls = diceRoll();
    printArray(rolls);
}

//I divided NUM_ROLLS by 500 because I didn't want it to crush.//
//My output is not correct.//

function diceRoll(){
    var rolls = [];
    for(var i = 0; i < NUM_ROLLS/500; i++){
        if(Randomizer.nextBoolean()){
            rolls.push([1]);
            rolls.push([2]);
            rolls.push([3]);
            rolls.push([4]);
            rolls.push([5]);
            rolls.push([6]);
        
        }
    }
    return rolls;
}

function printArray(arr){
    for(var i = 1; i < arr.length; i++){
        println(i + ": " + arr[i]);
    }
}

【问题讨论】:

  • 你的代码输出了什么?

标签: javascript arrays loops random


【解决方案1】:

最简单的方法是存储每个卷的计数,而不是将卷本身推送到数组中。然后要正确输出计数,您只需将卷数除以 40 并输出该数字多次。

var NUM_ROLLS = 10000;

function start() {
  var rolls = diceRoll();
  printArray(rolls);
}

function diceRoll() {
  //store the counts in an array
  var rolls = [0, 0, 0, 0, 0, 0];
  
  for (var i = 0; i < NUM_ROLLS; i++) {
    var result = parseInt(Math.random() * 6);
    rolls[result]++; //track the roll
  }
  return rolls;
}

function printArray(arr) {
  //output 1-6
  for (var i = 0; i < arr.length; i++) {
    //output the digit and count
    var line = (i + 1) + ": (" + arr[i] + ") ";
    //output a digit for every 40 rolls
    line += (i+1).toString().repeat(parseInt(arr[i] / 40));
    console.log(line);
  }
}

start();

【讨论】:

  • 谢谢,输出很完美,但是我不能使用:toString()、parseInt、Math.random() 或 console.log。它必须更基本。
  • “更基本”是什么意思?使用本机 JS 方法非常基本 - 没有外部依赖项,比编写自己的方法更易于维护等。你的 Randomizer 对象是什么?如果你愿意,你可以用你自己的打印方法替换console.log。这只是一个简单的功能示例,可以对其进行修改以满足您的特定需求。您可以用数组替换 rolls 计数器(就像您在问题中所说的那样),然后只需执行 rolls[1].length 即可获得计数。
  • 是的,我用过“println”
  • 这不是原生的 JS 方法,所以你需要确保它在别处定义才能使用它。否则,您可以改为使用document.write(line+"&lt;br&gt;");,尽管这通常不被视为一种输出方法。此外,如果您必须使用 Randomizer 并且不允许使用 Math.random(),则需要添加有关您的详细信息...尽管这超出了 SO.com 上的问题范围,因为这些应该是通用解决方案(原生 JS 是个好东西!),而不是针对单个特定作业问题的答案。
  • 感谢您的帮助。我想我得到的东西会很有用。
【解决方案2】:

我想我已经做到了:

var numROLLS = 10000;
var array;
function diceRoll() {
    array = [0, 0, 0, 0, 0, 0, 0, 0, 0];
    for (i = 0; i < numROLLS; i++) {
        array[Math.floor(Math.random() * 9)]++
    }
}
function printArray() {
    for (i = 0; i < array.length; i++){
        console.log((i+1) + ": (" + array[i] + ") " + (i+1).toString().padEnd(array[i] / 40, (i+1).toString()));
    }
}
function start() {
    diceRoll();
    printArray();
}
start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-29
    • 2014-10-07
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多