If/Else 语句仅在满足 else 条件的情况下运行
If/Else statement only runs if code even if else condition is satisfied
我正在尝试制作一个程序,讨论您最喜欢的科目以及您是右脑还是左脑。
function questionGame() {
var user = prompt("Are you RIGHT brained, LEFT brained, a bit of BOTH, or NEITHER?").toUpperCase();
switch (user){
case 'RIGHT':
var user1 = prompt("You like Art?").toUpperCase();
if (user1 = 'YES' || 'YEAH') {
alert("Sweet. You should check out LadyBeeSweets, she is the next michelangelo.");
} else if (user1 = 'NO') {
alert("Alright, ok, um, then do you like anything?");
} else {
alert("I'm sorry, I don't understand " + user1 + ", please respond YES or NO next time, thank you!");
};
break;
case 'LEFT':
var user2 = prompt("Oh nice! Me too, I love my tech! Do you have a Linux-Based Operating System?").toUpperCase();
if (user2 = 'YES') {
alert("Sweet, me too!");
} else if (user2 = 'NO') {
alert("Oh, you should definitely give one a try!")
} else {
alert("I'm sorry, I don't understand " + user2 +", next time please respond with YES or NO.");
};
break;
case 'BOTH':
var user3 = prompt("Interesting, so do you like Whales?").toUpperCase();
if (user3 = 'YES' || 'YEAH') {
alert("Cool.");
} else if (user3 = 'NO' || 'NOPE') {
alert("You, MONSTER!");
} else {
alert("Hmm, whats that? I don't know if you like whales or not. Please reply YES or NO next time!");
};
break;
case 'NEITHER':
var user4 = prompt("Hmm, ok. I'm a left brainer! Do you care that I'm a Left Brainer? YES or NO?").toUpperCase();
if (user4 = 'YES' || 'YEAH') {
alert("Thanks for caring!");
} else if (user4 = 'NO' || 'NOPE') {
alert("Wow, thanks a lot, bro.");
} else {
alert("I couldn't understand " + user4 + ", please reply YES or NO next time, thank you");
};
break;
default:
alert("I'm sorry, I don't understand " + user + ", try again and chose either 'RIGHT', 'LEFT', 'BOTH', or 'NEITHER'.");
questionGame();
};
};
但是,当您在第一个提示中输入 'Left' 时,它会询问您是否使用基于 linux 的操作系统,即使您键入 'no' 或类似 'guhgriubcrhmiecrc',它仍然运行第一个 if 语句的代码块。
我感谢所有答案。对了,这是JavaScript.
您在 if (user2 = 'YES')
等语句中使用赋值运算符 =
而不是比较运算符 ==
或 ===
正如 Jorg 提到的,您正在分配一个变量,而不是比较它。既然您已经确定这是您所做的事情,请通过将文字放在比较的左侧来避免将来这样做。
if ('YES' === user2) {
alert("Sweet, me too!");
}
// If you forget to use multiple equals again, this code will throw an error and the problem will be more obvious
if ('YES' = user2) {
alert("Oops!");
}
我正在尝试制作一个程序,讨论您最喜欢的科目以及您是右脑还是左脑。
function questionGame() {
var user = prompt("Are you RIGHT brained, LEFT brained, a bit of BOTH, or NEITHER?").toUpperCase();
switch (user){
case 'RIGHT':
var user1 = prompt("You like Art?").toUpperCase();
if (user1 = 'YES' || 'YEAH') {
alert("Sweet. You should check out LadyBeeSweets, she is the next michelangelo.");
} else if (user1 = 'NO') {
alert("Alright, ok, um, then do you like anything?");
} else {
alert("I'm sorry, I don't understand " + user1 + ", please respond YES or NO next time, thank you!");
};
break;
case 'LEFT':
var user2 = prompt("Oh nice! Me too, I love my tech! Do you have a Linux-Based Operating System?").toUpperCase();
if (user2 = 'YES') {
alert("Sweet, me too!");
} else if (user2 = 'NO') {
alert("Oh, you should definitely give one a try!")
} else {
alert("I'm sorry, I don't understand " + user2 +", next time please respond with YES or NO.");
};
break;
case 'BOTH':
var user3 = prompt("Interesting, so do you like Whales?").toUpperCase();
if (user3 = 'YES' || 'YEAH') {
alert("Cool.");
} else if (user3 = 'NO' || 'NOPE') {
alert("You, MONSTER!");
} else {
alert("Hmm, whats that? I don't know if you like whales or not. Please reply YES or NO next time!");
};
break;
case 'NEITHER':
var user4 = prompt("Hmm, ok. I'm a left brainer! Do you care that I'm a Left Brainer? YES or NO?").toUpperCase();
if (user4 = 'YES' || 'YEAH') {
alert("Thanks for caring!");
} else if (user4 = 'NO' || 'NOPE') {
alert("Wow, thanks a lot, bro.");
} else {
alert("I couldn't understand " + user4 + ", please reply YES or NO next time, thank you");
};
break;
default:
alert("I'm sorry, I don't understand " + user + ", try again and chose either 'RIGHT', 'LEFT', 'BOTH', or 'NEITHER'.");
questionGame();
};
};
但是,当您在第一个提示中输入 'Left' 时,它会询问您是否使用基于 linux 的操作系统,即使您键入 'no' 或类似 'guhgriubcrhmiecrc',它仍然运行第一个 if 语句的代码块。 我感谢所有答案。对了,这是JavaScript.
您在 if (user2 = 'YES')
=
而不是比较运算符 ==
或 ===
正如 Jorg 提到的,您正在分配一个变量,而不是比较它。既然您已经确定这是您所做的事情,请通过将文字放在比较的左侧来避免将来这样做。
if ('YES' === user2) {
alert("Sweet, me too!");
}
// If you forget to use multiple equals again, this code will throw an error and the problem will be more obvious
if ('YES' = user2) {
alert("Oops!");
}