1 到 20 之间的 3 个随机数。将第一个数字与第二个和第三个数字进行比较
3 random numbers between 1 and 20. Compare first number to second and third
我必须编写一个程序来生成 3 个介于 1 和 20 之间的随机数,并且在单击按钮时需要将第一个数字与其他两个数字进行比较。输出应显示所有 3 个数字并指示比较结果,例如: 1) 第一个数字小于第二个和第三个数字; 2)第一个数在其他两个数之间; 3)第一个数字大于其他两个; 4) 第一个数等于另外两个数中的一个;或 5) 所有三个数字都相等。
我通常是编程新手,因此非常感谢任何帮助。
Math.random()
将 return 一个介于 0 和 1 之间的数字(带小数点)。
对于特定的数字范围,您可以执行以下操作:
var x = Math.floor((Math.random() * y) + z);
其中 y
是 Max (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';
}
等等...
<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>
<!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>
我必须编写一个程序来生成 3 个介于 1 和 20 之间的随机数,并且在单击按钮时需要将第一个数字与其他两个数字进行比较。输出应显示所有 3 个数字并指示比较结果,例如: 1) 第一个数字小于第二个和第三个数字; 2)第一个数在其他两个数之间; 3)第一个数字大于其他两个; 4) 第一个数等于另外两个数中的一个;或 5) 所有三个数字都相等。
我通常是编程新手,因此非常感谢任何帮助。
Math.random()
将 return 一个介于 0 和 1 之间的数字(带小数点)。
对于特定的数字范围,您可以执行以下操作:
var x = Math.floor((Math.random() * y) + z);
其中 y
是 Max (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';
}
等等...
<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>
<!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>