简体   繁体   English

我在这里做错了什么? 否则

[英]What am I doing wrong here? Else if

So I'm having trouble here. 所以我在这里遇到麻烦。 It's an assignment, basically I ask a question of what 3+3 is and the user can put the right answer in... 6 and it will say "Correct!" 这是一项作业,基本上我问一个3 + 3是什么的问题,用户可以在... 6中输入正确的答案,然后说“正确!”。 If it's not a number, then it will say "please type in a number..." If it's 5 or 7 then it will say "Very close, try again" if it's anything other than 5 6 or 7 it wills say "incorrect" and if nothing is type in it should say "come on, you can do it." 如果不是数字,则显示“请输入数字...”。如果是5或7,则显示“非常接近,请重试”。如果不是5 6或7,则显示“不正确” ”,如果没有输入任何内容,则应该说“来吧,你可以做到”。 What am I doing wrong here???? 我在这里做错了什么???? Currently all it's doing is saying yes, 6 is correct! 目前所做的只是说是,正确的是6! even when i type in a different number 即使我输入其他数字

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);

In your code you user question=8 in if which mean you assign 8 to question. 在您的代码中,您使用question = 8表示是否将8分配给问题。

= means assign and == mean comparing =表示分配,==表示比较

Try this : 尝试这个 :

 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); 

As noted in @Anik Islam Abhi's answer and the comments = is not the same as == . 正如@Anik Islam Abhi的回答中所指出的,注释= ==

== is a comparison operator ( read more ) ==是比较运算符( 了解更多

= is an assignment operator ( read more ) =是赋值运算符( 了解更多信息

Now, what you need to do is decide what means for the user to enter nothing ? 现在,您需要确定什么让用户什么都不输入? I will assume you mean any amount of whitespace. 我将假设您的意思是任何空白。

What you can do is always remove all whitespace from the inputed answer and if the user still inputed nothing then you know you should print "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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM