我在这里做错了什么?否则如果
What am I doing wrong here? Else if
所以我在这里遇到了麻烦。这是一个作业,基本上我问的问题是 3+3 是什么,用户可以将正确答案输入... 6,它会说 "Correct!" 如果它不是数字,那么它会说 "please type in a number..." 如果它是 5 或 7 那么它会说 "Very close, try again" 如果它不是 5 6 或 7 它会说 "incorrect" 如果没有输入它应该说 "come on, you can do it." 我是什么在这里做错了????目前它所做的只是说是,6 是正确的!即使我输入不同的数字
var question;
question = window.prompt("What's the sum of 3+3?", "");
question = parseFloat(question);
if (isNaN(question)) {
output = "Please enter a number";
} else if (question = 6) {
output = "Yes " + question + " is correct!";
} else if (question = 5) {
output = "Very close, try again!";
} else if (question = 7) {
output = "Very close, try again!";
} else if (question = null) {
output = "Come on, you can do it!!";
} else {
output="Incorrect, Please try again"
}
document.write(output);
在您的代码中,您在 if 中使用了 question=8,这意味着您将 8 分配给了 question。
= means assign and == mean comparing
试试这个:
var question;
question = window.prompt("What's the sum of 3+3?","");
question = parseFloat(question);
if (isNaN(question))
{
output= "Please enter a number";
}else if (question==6)
{
output="Yes " +question+" is correct!";
}else if (question==5){
output="Very close, try again!";
}else if (question==7){
output="Very close, try again!";
}else if (question==null){
output="Come on, you can do it!!";
}
else {output="Incorrect, Please try again"}
document.write(output);
如@Anik Islam Abhi 的回答和评论所述 =
与 ==
不同。
==
是比较运算符(read more)
=
是赋值运算符 (read more)
现在,您需要做的是决定用户输入nothing的方式是什么?
我假设你指的是任意数量的空格。
你可以做的是始终从输入的答案中删除所有空格,如果用户仍然输入 nothing 那么你知道你应该打印 "Come on, you can do it!!"
// get the question and remove all whitespace so we know if the user enter an empty string
var question = window.prompt("What's the sum of 3+3?","").trim().replace(' ','');
if (!question) { // nothing
output = "Come on, you can do it!!";
} else if(isNaN(question)) { // not a number
output = "Please enter a number";
} else if (question == 6) { // correct answer
output = "Yes " +question+" is correct!";
} else if (question == 5 || question == 7) { // close answer
output = "Very close, try again!";
} else {
output="Incorrect, Please try again" // incorrect answer
}
document.write(output);
所以我在这里遇到了麻烦。这是一个作业,基本上我问的问题是 3+3 是什么,用户可以将正确答案输入... 6,它会说 "Correct!" 如果它不是数字,那么它会说 "please type in a number..." 如果它是 5 或 7 那么它会说 "Very close, try again" 如果它不是 5 6 或 7 它会说 "incorrect" 如果没有输入它应该说 "come on, you can do it." 我是什么在这里做错了????目前它所做的只是说是,6 是正确的!即使我输入不同的数字
var question;
question = window.prompt("What's the sum of 3+3?", "");
question = parseFloat(question);
if (isNaN(question)) {
output = "Please enter a number";
} else if (question = 6) {
output = "Yes " + question + " is correct!";
} else if (question = 5) {
output = "Very close, try again!";
} else if (question = 7) {
output = "Very close, try again!";
} else if (question = null) {
output = "Come on, you can do it!!";
} else {
output="Incorrect, Please try again"
}
document.write(output);
在您的代码中,您在 if 中使用了 question=8,这意味着您将 8 分配给了 question。
= means assign and == mean comparing
试试这个:
var question;
question = window.prompt("What's the sum of 3+3?","");
question = parseFloat(question);
if (isNaN(question))
{
output= "Please enter a number";
}else if (question==6)
{
output="Yes " +question+" is correct!";
}else if (question==5){
output="Very close, try again!";
}else if (question==7){
output="Very close, try again!";
}else if (question==null){
output="Come on, you can do it!!";
}
else {output="Incorrect, Please try again"}
document.write(output);
如@Anik Islam Abhi 的回答和评论所述 =
与 ==
不同。
==
是比较运算符(read more)
=
是赋值运算符 (read more)
现在,您需要做的是决定用户输入nothing的方式是什么? 我假设你指的是任意数量的空格。
你可以做的是始终从输入的答案中删除所有空格,如果用户仍然输入 nothing 那么你知道你应该打印 "Come on, you can do it!!"
// get the question and remove all whitespace so we know if the user enter an empty string
var question = window.prompt("What's the sum of 3+3?","").trim().replace(' ','');
if (!question) { // nothing
output = "Come on, you can do it!!";
} else if(isNaN(question)) { // not a number
output = "Please enter a number";
} else if (question == 6) { // correct answer
output = "Yes " +question+" is correct!";
} else if (question == 5 || question == 7) { // close answer
output = "Very close, try again!";
} else {
output="Incorrect, Please try again" // incorrect answer
}
document.write(output);