带有 if 语句的 do-while 循环中的随机数
Random Number in a do-while loop with if statement
我正在尝试制作一个随机数生成器,生成一串介于 1 和 9 之间的数字,如果它生成一个 8,它应该最后显示 8,然后停止生成。
到目前为止它打印出 1 2 3 4 5 6 7 8,但它不会生成随机数字串,所以我需要知道如何使循环实际生成如上所述的随机数,感谢任何求助!
Javascript
// 5. BONUS CHALLENGE: Write a while loop that builds a string of random
integers
// between 0 and 9. Stop building the string when the number 8 comes up.
// Be sure that 8 does print as the last character. The resulting string
// will be a random length.
print('5th Loop:');
text = '';
// Write 5th loop here:
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
i = 0;
do {
i += 1;
if (i >= 9) {
break;
}
text += i + ' ';
} while (i <= 9);
print(text); // Should print something like `4 7 2 9 8 `, or `9 0 8 ` or `8
`.
你可以用更简单的方式来做:
解决方法是push
把随机生成的数放到一个数组中,然后用join
方法join数组的所有元素需要的字符串。
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
var array = [];
do {
random = getRandomNumber(9);
array.push(random);
} while(random != 8)
console.log(array.join(' '));
print() 是一个函数,其目标是打印文档,您应该使用 console.log() 在控制台中显示。
在循环之前放一个布尔值,例如 var eightAppear = false
你现在的情况看起来像do {... }while(!eightAppear)
然后在你的循环中生成一个 0 到 9 之间的随机数。Math.floor(Math.random()*10)
连接你的字符串。如果数字是 8,则将 eightAppear
的值更改为 true
既然是练习题,那我就让你编码吧,现在应该不难了:)
不是因为它更好,而是因为我们可以(而且我喜欢生成器 :)),一个带有迭代器函数的替代方案(需要 ES6):
function* getRandomNumbers() {
for(let num;num !==8;){
num = Math.floor((Math.random() * 9) + 1);
yield num;
}
}
let text= [...getRandomNumbers()].join(' ');
console.log(text);
这是实现此目的的另一种方法。在这里,我创建了一个变量 i 并将随机数存储在其中,然后我创建了 while 循环。
i = Math.floor(Math.random() * 10)
while (i !== 8) {
text += i + ' ';
i = Math.floor(Math.random() * 10)
}
text += i;
console.log(text);
这是同样的事情,但作为一个 do...while 循环。
i = Math.floor(Math.random() * 10)
do {
text += i + ' ';
i = Math.floor(Math.random() * 10)
} while (i !== 8)
text += i;
console.log(text);
我正在尝试制作一个随机数生成器,生成一串介于 1 和 9 之间的数字,如果它生成一个 8,它应该最后显示 8,然后停止生成。
到目前为止它打印出 1 2 3 4 5 6 7 8,但它不会生成随机数字串,所以我需要知道如何使循环实际生成如上所述的随机数,感谢任何求助!
Javascript
// 5. BONUS CHALLENGE: Write a while loop that builds a string of random
integers
// between 0 and 9. Stop building the string when the number 8 comes up.
// Be sure that 8 does print as the last character. The resulting string
// will be a random length.
print('5th Loop:');
text = '';
// Write 5th loop here:
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
i = 0;
do {
i += 1;
if (i >= 9) {
break;
}
text += i + ' ';
} while (i <= 9);
print(text); // Should print something like `4 7 2 9 8 `, or `9 0 8 ` or `8
`.
你可以用更简单的方式来做:
解决方法是push
把随机生成的数放到一个数组中,然后用join
方法join数组的所有元素需要的字符串。
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
var array = [];
do {
random = getRandomNumber(9);
array.push(random);
} while(random != 8)
console.log(array.join(' '));
print() 是一个函数,其目标是打印文档,您应该使用 console.log() 在控制台中显示。
在循环之前放一个布尔值,例如 var eightAppear = false
你现在的情况看起来像do {... }while(!eightAppear)
然后在你的循环中生成一个 0 到 9 之间的随机数。Math.floor(Math.random()*10)
连接你的字符串。如果数字是 8,则将 eightAppear
的值更改为 true
既然是练习题,那我就让你编码吧,现在应该不难了:)
不是因为它更好,而是因为我们可以(而且我喜欢生成器 :)),一个带有迭代器函数的替代方案(需要 ES6):
function* getRandomNumbers() {
for(let num;num !==8;){
num = Math.floor((Math.random() * 9) + 1);
yield num;
}
}
let text= [...getRandomNumbers()].join(' ');
console.log(text);
这是实现此目的的另一种方法。在这里,我创建了一个变量 i 并将随机数存储在其中,然后我创建了 while 循环。
i = Math.floor(Math.random() * 10)
while (i !== 8) {
text += i + ' ';
i = Math.floor(Math.random() * 10)
}
text += i;
console.log(text);
这是同样的事情,但作为一个 do...while 循环。
i = Math.floor(Math.random() * 10)
do {
text += i + ' ';
i = Math.floor(Math.random() * 10)
} while (i !== 8)
text += i;
console.log(text);