[英]Javascript Switch Statement with more than one default
I'm a beginner at JavaScript. 我是JavaScript的初学者。 I'm sorry if I cannot explain clearly what I need. 很抱歉,如果我不能清楚地说明我的需求。
I am trying to design a page with some questions. 我正在尝试设计一个有一些问题的页面。 The answers must be typed in a textbox. 答案必须在文本框中输入。
I am using a Switch Statement to generate different comments to all acceptable answers. 我正在使用Switch语句为所有可接受的答案生成不同的注释。
As for answers that are not accepted, I would like to have more than the default message. 至于不被接受的答案,除了默认消息外,我还想得到更多。
For example, if the user types an unaccepted answer for the first time, a message will show up, like "That is not an acceptable answer". 例如,如果用户第一次键入不可接受的答案,则会显示一条消息,例如“那不是可接受的答案”。 On the user's second unaccepted answer a different message would show up, like "Please try again"... And so on for about five times, and then it would loop back to the first default message. 在用户的第二个不可接受的答案上,将显示不同的消息,例如“请重试” ...等等,大约五次,然后它将循环回到第一个默认消息。
I just don't know how to make that happen... This is what I have so far: 我只是不知道该如何实现...这是我到目前为止的事情:
function myFunction() { var text; var colors = document.getElementById("myInput").value; switch (colors) { case "White": text = "White is a nice color."; break; case "Blue": text = "I also like blue. It reminds me of the ocean."; break; case "Red": text = "Red is also nice."; break; default: text = "That is not an acceptable answer"; } document.getElementById("comment").innerHTML = text; }
<p>What is your favorite color from the USA flag?</p> <input id="myInput" type="text"> <button onclick="myFunction()">Answer</button> <p id="comment"></p>
You need to have an array for the number of messages, the user needs to get when they have sent an answer. 您需要一个用于消息数量的数组,用户需要在发送答案后获取消息。
var count = 0;
var messages = ["That is not an acceptable answer.", "Please try again!", "Still wrong.", "I don't understand.", "Consider visiting the 'help page' before moving on."];
Based on that count, show the message in the default
case. 根据该计数,在default
情况下显示该消息。
default:
text = messages[count%messages.length];
count++;
Full Working Snippet 完整的摘要
var count = 0; var messages = ["That is not an acceptable answer.", "Please try again!", "Still wrong.", "I don't understand.", "Consider visiting the 'help page' before moving on."]; function myFunction() { var text; var colors = document.getElementById("myInput").value; switch (colors) { case "White": text = "White is a nice color."; break; case "Blue": text = "I also like blue. It reminds me of the ocean."; break; case "Red": text = "Red is also nice."; break; default: text = messages[count%messages.length]; count++; } document.getElementById("comment").innerHTML = text; }
<p>What is your favorite color from the USA flag?</p> <input id="myInput" type="text"> <button onclick="myFunction()">Answer</button> <p id="comment"></p>
var counter = 0;
function myFunction() {
var text;
var colors = document.getElementById("myInput").value;
switch(colors) {
case "White":
text = "White is a nice color.";
break;
case "Blue":
text = "I also like blue. It reminds me of the ocean.";
break;
case "Red":
text = "Red is also nice.";
break;
default:
text = getText(counter++);
}
counter = counter > 5 ? 0 : counter;
document.getElementById("comment").innerHTML = text;
}
function getText(counter) {
switch(counter):
case 1:
return "some text";
case 2:
return "some text";
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.