gpt4 book ai didi

javascript - switch 语句中的大于和小于

转载 作者:行者123 更新时间:2023-11-28 14:35:40 25 4
gpt4 key购买 nike

我试图找出为什么这段代码不起作用

let userName = 'roy';
const userQuestion = 'Do you do CrossFit?';
userName ? console.log('Hello ' + userName + ' !') : console.log('Hello!');
console.log(`The user asked: ${userQuestion}`);
let randomNumber = Math.floor(Math.random() * 8);
let eightBall = '';
console.log(randomNumber);

switch (randomNumber) {
case 0:
eightBall = 'It is certain';
break;
case 1:
eightBall = 'It is defidedly so';
break;
case randomNumber >= 1:
eightBall = 'we cant tell';
break;
}

console.log(` The eight ball answered: ${eightBall}`);

当数字大于 1 时,尝试生成“我们可以判断”,但它没有打印任何内容。我使用 switch 语句是否错误?

最佳答案

使用默认值和内部的 if 语句,如下所示:

let randomNumber = 100
let eightBall = ''

switch (randomNumber) {
case 0:
eightBall = 'It is certain';
break;
case 1:
eightBall = 'It is defidedly so';
break;
default:
if(randomNumber >= 1)
eightBall = 'we cant tell';
break;
}

console.log(` The eight ball answered: ${eightBall}`);

关于javascript - switch 语句中的大于和小于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49881732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com