【问题标题】:3 random numbers between 1 and 20. Compare first number to second and third [closed]1 到 20 之间的 3 个随机数。将第一个数字与第二个和第三个数字进行比较 [关闭]
【发布时间】:2015-12-06 23:53:27
【问题描述】:

我必须编写一个程序来生成 3 个介于 1 和 20 之间的随机数,并且在单击按钮时需要将第一个数字与其他两个数字进行比较。输出应显示所有 3 个数字并指示比较结果,例如: 1) 第一个数字小于第二个和第三个数字; 2)第一个数字在其他两个数字之间; 3)第一个数字大于其他两个; 4)第一个数字等于其他两个数字之一;或 5) 所有三个数字都相等。

我通常是编程新手,因此感谢您提供任何帮助。

【问题讨论】:

  • 首先尝试一些东西并发布您的尝试,然后寻求建议。

标签: javascript html random generator


【解决方案1】:
<html>
    <head>
        <script>
            function compare() {
                var a = Math.floor((Math.random() * 20) + 1);
                var b = Math.floor((Math.random() * 20) + 1);
                var c = Math.floor((Math.random() * 20) + 1);
                var message = "1st - "+a+"; 2st - "+b+"; 3st - "+c+"\n";
                if (a < b && a < c) {
                    message += 'The first number is less than both the second and third numbers';
                } else if ((a < b && a > c) || (a < c && a > b)) {
                    message += 'The first number is between the other two numbers';
                } else if (a > b && a > c) {
                    message += 'The first number is greater than the other two';
                } else if (a == b && b == c) {
                    message += 'All three numbers are equal';
                } else {
                    message += "The first number is equal to one of the other two numbers"
                }
                alert(message);
            }
        </script>
    </head>
    <body>
        <button type="button" onClick="compare();">Click Me!</button>
    </body>
</html>

【讨论】:

  • 谢谢你帮了我很多
【解决方案2】:

Math.random() 将返回一个介于 0 和 1 之间的数字(带小数)。 对于特定的数字范围,您可以这样做:

var x = Math.floor((Math.random() * y) + z);

yMax (max - min + 1),z 是最小值。对于你的例子,你会这样做:

var x = Math.floor((Math.random() * 20) + 1);

至于比较,因为有很多场景你应该使用if-else 语句,所以它看起来像:

var x = Math.floor((Math.random() * 20) + 1);
var y = Math.floor((Math.random() * 20) + 1);
var z = Math.floor((Math.random() * 20) + 1);
var output = null;
if(x > y && x > z){
  output = 'The first number is greater than the other two';
}else if(x < y && x < z){
  output = 'The first number is less than the other two';
}

等等……

【讨论】:

  • 好的,谢谢。我试试这个
  • 随机表达式中的y 不是最大值,而是范围(max - min + 1)。例如,如果您想要一个介于 10 和 20 之间的随机数,则范围为 11,因此您使用 Math.floor(Math.random() * 11) + 10
【解决方案3】:
<!DOCTYPE html>
<html>
<body>

<p>Click the button to display a random number between 1 and 20.</p>

<button onclick="x()">Roll</button>
<button onclick="y()">Roll</button>
<button onclick="z()">Roll</button>

<p id="demo"></p>

    <script>
var x = Math.floor((Math.random() * 20) + 1);
var y = Math.floor((Math.random() * 20) + 1);
var z = Math.floor((Math.random() * 20) + 1);
var output = null;
if(x > y && x > z){
  output = 'The first number is greater than the other two';
}else if(x < y && x < z){
  output = 'The first number is less than the other two';
}
</script>

</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多