我需要一个JavaScript代码,生成1到10次之间的3个数字。它应该在用逗号分隔的新行上显示每组3个数字,并且还显示生成的数字等于的总数。 7(也在 一条单独的线)。 示例输出应格式如下: 数字集1是:10,7,8 数字集2是:5,1,7 我出于某种原因的代码不起作用
<html>
<head>
<title>Day 3 - Example 7</title>
</head
<body>
<script language="javascript">
// count number of times seven was generated
var i,num,n,num1,num2,cnt=0;
n=100;
for( i=1; i<=n; i++){
num = Math.floor(Math.random()*10+1);
num1 = Math.floor(Math.random()*10+1);
num2 = Math.floor(Math.random()*10+1);
document.write("Number Set " +i+ is + num,+ num1, +num2);
if (num == 7) {
cnt++;
> }
}
document.write("<br>Total number of Sevens: " + cnt);
</script>
</body>
</html
答案 0 :(得分:1)
您需要在document.write()
来电中引用字符串。
document.write("Number Set " +i+ " is " + num + ", " + num1 + ", " +num2 + '<br>');
答案 1 :(得分:1)
您发布的代码似乎是基于您的描述,除了一些小错误。我已将它清理干净,并在Stack Overflow中使其可以运行。
var i, num, n, num1, num2, cnt = 0;
n = 100;
for (i = 1; i <= n; i++) {
num = Math.floor(Math.random() * 10 + 1);
num1 = Math.floor(Math.random() * 10 + 1);
num2 = Math.floor(Math.random() * 10 + 1);
console.log("Number Set", i, "is", num, num1, num2);
if (num == 7) {
cnt++;
}
}
console.log("<br>Total number of Sevens: " + cnt);
&#13;
注意:当您在字符串和数字之间使用+
时,数字将转换为字符串,然后将附加两个字符串。逗号(,
)将追加由空格分隔的值。我一直用逗号来表达清晰和一致。为了便于阅读,我还考虑了一些问题并使用了console.log
,因此我们可以看到格式更改的结果。
答案 2 :(得分:1)
您的代码存在一些问题:
1)您正在尝试将字符串发送到document.write但尚未正确格式化。任何只是字符串而不是某种变量的东西都需要用引号括起来。在您的情况下,这包括:&#39;是&#39;和逗号(&#39;,&#39;)。
(我已将每一行放在<p>
标记内,以确保将其输出到换行符)
2) 在您的代码末尾还有一个需要删除的迷路>
字符。
以下是修改后代码的示例:
var i,num,n,num1,num2,cnt=0;
n=100;
for( i=1; i<=n; i++){
num = Math.floor(Math.random()*10+1);
num1 = Math.floor(Math.random()*10+1);
num2 = Math.floor(Math.random()*10+1);
document.write("<p>Number Set " + i + " is " + num + ", " + num1 + ", " + num2 + "</p>");
if(num == 7){
cnt++;
}
}
document.write("<br>Total number of Sevens: " + cnt);
&#13;
答案 3 :(得分:1)
你有几个问题:
<head>
元素未正确关闭。请考虑以下插图:
<html>
<head>
<title>Day 3 - Example 7</title>
</head>
<body>
<script language="javascript">
// count number of times seven was generated
var i,num,n,num1,num2,cnt=0;
n=100;
for( i=1; i<=n; i++){
num = Math.floor(Math.random()*10+1);
num1 = Math.floor(Math.random()*10+1);
num2 = Math.floor(Math.random()*10+1);
console.log("Number Set " +i + " is --> "+ num +", "+ num1 +" ," +num2);
if(num == 7){
cnt++;
}
}
document.write("<br>Total number of Sevens: " + cnt);
</script>
</body>
</html
&#13;
您可以将console.log()
替换为您想要的内容:
document.write("Number Set " +i+ " is " + num + ", " + num1 + ", " +num2);
答案 4 :(得分:1)
var n = 100,
counter = 0;
for (var i = 1; i <= n; i++) { // repeat this n times
var text = "Number Set " + i + " is:"; // text of this set
for(var j = 0; j < 3; j++) { // repeat this 3 times
var num = Math.floor(Math.random() * 10) + 1; // get a random number between 0 an 10
text += (j? ", ": " ") + num; // add it to this set's text
if(num === 7) // if the random number is seven
counter++; // then increment the counter
}
document.write(text + "<br>"); // show this set's text
}
document.write("Total number of Sevens: " + counter + "<br>"); // the seven's count
答案 5 :(得分:0)
使用以下行:document.write("Number Set "+i+" is "+ num +","+num1 +","+num2);
答案 6 :(得分:0)
足够的评论者已经指出了文档中的错误 - 我只是添加了一个版本,以更优雅的方式解决问题。
var setSize = 3,
totalSize = 100,
magicNumber = 7,
magicCounter = 0,
i = setSize * totalSize,
set = [];
do {
// push random number into set
set.push(Math.floor(Math.random() * 10 + 1));
// decrement the counter
i -= 1;
// jump to the next iteration, if the set is not complete
if (set.length !== setSize) {
continue;
}
console.log("Number set is", set.join(', '));
// reduce the array into the sum of items and check if equal 7
if (set.reduce(function(a, b) { return a + b; }, 0) === magicNumber) {
magicCounter += 1;
}
// reset the set for the next line
set = [];
} while (i > 0);
console.log("Total number of ", magicNumber, ":", magicCounter);