我正在尝试使用分支答案系统创建一个基本的文字游戏。关于提示,我马上遇到了一个问题。当我写下我的' if'声明带来另一个提示,没有任何反应。我怎样才能解决这个问题?以下是我的工作样本:
confirm("You wake up to your mother's voice, 'Wake up! I can't believe you slept in this late! You need to get dressed and hurry on down to Professor Oak's Lab! No time for breakfast! Get going!'");
var questionOne = prompt("1. Hurry out of bed, quickly get dressed, and run out the door! 2. Roll out of bed sleepily, manage to put on your clothes, and make a cup of coffee before leaving the house. 3. Grumble back at your mother and go back to sleep." , "Enter 1, 2, or 3.");
if (questionOne === 1)
{
prompt("You arrive at Professor Oak's lab in a rush, but haven't quite missed the event! It's time to get your first Pokemon! When you meet the Professor he says with a wink, 'Ah yes, it's you! I remember your mother quite well. Wonder woman... Alright, follow me!' You follow Oak into his lab to find 3 Pokeballs on his desk. 'Choose one,' he says with a hand gesture. [1. Left Pokeball. 2. Middle Pokeball. 3. Right Pokeball.]");
}
答案 0 :(得分:0)
提示返回字符串值。您需要将提示返回值转换为整数。或者相反,与字符串"1"
进行比较。
选项1:
if (parseInt(questionOne) === 1) {
prompt("Second tree.")
}
选项2:
if (questionOne === '1') {
prompt("Second tree.")
}
答案 1 :(得分:0)
prompt
函数返回一个字符串(因为你可以添加你想要的任何文本)。
因此,既然您正在使用===
操作 - 而且它没有进行隐含的转换 - 当您尝试将结果与Number
进行比较时,{{1永远不会经历。
您必须将您的比较更改为进行隐含转换的if-statement
运算符,或者您可以将其直接更改为字符串,例如==
。看一下下面的例子:
questionOne === '1'