【发布时间】:2017-01-16 21:57:33
【问题描述】:
我正在尝试制作一个程序来谈论您最喜欢的科目以及您是右脑还是左脑。
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。
【问题讨论】:
-
user1 = 'YES'不是比较运算,它是一个赋值,计算结果为真 -
正如@PatrickEvans 所说,应该使用类似 if (user1 === 'YES' || user1 === 'YEAH') {
-
成功了!谢谢你们,我应该知道这与 = 符号有关。
标签: javascript if-statement switch-statement prompt