[英]How to get Rid of this Nan?
我正在构建一个只有 Html 和 Javascript 的骰子。 我想要两个随机生成的数字(相加)的结果。 我怎样才能得到结果作为两个数字的总和。
<!DOCTYPE html>
<html>
<head>
<title>Dice</title>
<h1 style="text-align:center;color:lime;font-size:300%">Dice</h1>
<hr/>
<br/>
<h1 style="color: red;text-align:center;font-size:250%">You Have Got:</h1>
<h1 style="color:gold;text-align:center;font-size:300%"><script type="text/javascript">
var num1 = document.write(Math.floor(Math.random() * 6 + 1));
document.write("<br/>");
document.write("<br/>");
var nu,2 = document.write(Math.floor(Math.random() * 6 + 1));
document.write("<br/>");
num1 = parseFloat(num1);
num2 = parseFloat(num2
);
document.write("<br/>");
document.write("Result:");
document.write(dice1 + dice2);
</script></h1>
<button onClick="window.location.reload();" style="font-size:200%; margin:auto;display:block;background-color:black;color:cyan">Roll Again</button>
</head>
<body style="background-color:black">
</body>
</html>
所以如果我运行这个程序。 我得到的结果为 NaN。 我试过了
parseInt();
parseFloat();
我只会将数字存储为单独的变量,因此您不必在添加之前进行转换。
<!DOCTYPE html>
<html>
<head>
<title>Dice</title>
<h1 style="text-align:center;color:lime;font-size:300%">Dice</h1>
<hr />
<br />
<h1 style="color: red;text-align:center;font-size:250%">You Have Got:</h1>
<h1 style="color:gold;text-align:center;font-size:300%">
<script type="text/javascript">
var num1 = Math.floor(Math.random() * 6 + 1);
document.write(num1);
document.write("<br/>");
document.write("<br/>");
var num2 = Math.floor(Math.random() * 6 + 1);
document.write(num2);
document.write("<br/>");
//num1 = parseFloat(num1);
//num2 = parseFloat(num2);
document.write("<br/>");
document.write("Result:");
document.write(num1 + num2);
</script>
</h1>
<button onClick="window.location.reload();"
style="font-size:200%; margin:auto;display:block;background-color:black;color:cyan">Roll Again</button>
</head>
<body style="background-color:black">
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.