gpt4 book ai didi

Javascript switch 语句不起作用 - 三个太多了吗?

转载 作者:行者123 更新时间:2023-11-30 20:21:51 26 4
gpt4 key购买 nike

<分区>

我是 Javascript 的新手,我正在尝试通过制作一个小程序来学习,您可以在其中输入您的星座、行星和宫位来制作一些疯狂的故事。为此,我将三个 switch 语句放在一个函数中。

不确定那是否是太多的 switch 语句,但是当我在 Chrome 上加载它时,有时我只得到文本,有时我得到文本和文本 2,有时我只得到文本 3。

有人知道为什么会这样吗?我只是在一个函数中有太多的 switch 语句吗?我在网上搜索,但找不到一个函数允许多少个。

function tellMeaStory() {
var text;
var text2;
var text3;
var sign = document.getElementById("mySign").value;
var planet = document.getElementById("myPlanet").value;
var house = document.getElementById("myHouse").value;

switch(sign) {
case "Aries", "aries":
text = "red";
break;
case "Taurus", "taurus":
text = "calm";
break;
case "Gemini", "gemini":
text = "quirky";
break;
case "Cancer", "cancer":
text = "moody";
break;
case "Leo", "leo":
text = "hungry";
break;
default:
text = "sign";
}

switch(planet) {
case "Sun", "sun":
text2 = "hero";
break;
case "Moon", "moon":
text2 = "mother";
break;
case "Mercury", "mercury":
text2 = "twin";
break;
case "Venus", "venus":
text2 = "courtesan";
break;
case "Mars", "mars":
text2 = "soldier";
break;
default:
text2 = "planet";
}

switch(house) {
case "one", "One":
text3 = "hole";
break;
case "two", "Two":
text3 = "buffet";
break;
case "three", "Three":
text3 = "database";
break;
case "four", "Four":
text3 = "stomach";
break;
case "five", "Five":
text3 = "wilderness";
break;
default:
text3 = "house";
}
document.getElementById("demo").innerHTML += "You are a " + text + " " + text2 + " in a " + text3;
}
<input id="mySign" type="text" value="Enter your sign">
<input id="myPlanet" type="text" value="Enter your planet">
<input id="myHouse" type="text" value="Enter your house">

<button onclick="tellMeaStory()">Generate</button>
<div id="demo"></div>

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com