用户输入和多个switch语句

时间:2018-04-20 04:47:48

标签: javascript

询问用户输入后,如果答案是"是"如何进行切换?如果答案是"没有"?我认为它需要写成if / else,但我不确定格式。

问题:你想吃披萨吗? 如果为真:执行大小切换 如果为假:做一个蛋糕味道的开关

1 个答案:

答案 0 :(得分:0)

也许这会给你一个想法。您也可以将else / if替换为switch,将switch替换为else / if

function go() {
  var input = document.getElementById('input').value;

  if (input.toLowerCase() == "yes") {
    // Here comes the switch for the size
    var size = prompt("Enter size:");
    console.log(size);
    switch (size) {
      case "20":
        alert("Okay, size will be 20");
        break;
      case "25":
        alert("Okay, size will be 25");
        break;
      default:
        alert("Size must be 20 or 25");
    }
  } else if (input.toLowerCase() == "no") {
    // And here the switch for the flavor
    var flavor = prompt("Please enter the flavor:");
    switch (flavor) {
      case "whatever":
        alert("Okay, it will taste like whatever");
        break;
      case "some":
        alert("Okay, it will taste like some");
        break;
      default:
        alert("It must taste like either whatever or some");
    }
  }
}
Do you want pizza?
<input id="input" type="text">
<button onclick="go()">Submit</button>