我正在尝试制作一个程序来讨论你最喜欢的科目,如果你是正确的或左脑的话。
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。
答案 0 :(得分:4)
您在=
==
而不是比较运算符===
或if (user2 = 'YES')
答案 1 :(得分:0)
正如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!");
}