第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我想為我的 switch 語句添加更多選項(xiàng)。有沒有辦法做到這一點(diǎn)?

我想為我的 switch 語句添加更多選項(xiàng)。有沒有辦法做到這一點(diǎn)?

蝴蝶刀刀 2022-06-16 10:47:08
我有這個(gè)代碼:<input id = "input" type = "text" placeholder = "Type 'Help1' for actions"/> <button onclick="button()">Confirm</button><p id="message"></p><script>function button() {   var text;   var textInput = input.value;   switch (textInput) {        case "Help1":            text = "[Page 1 of 2. Type 'Help2' for page 2] Commands you can use: <ul><li>South</li><li>North</li><li>East</li><li>West</li><li>North Again</li><li>South again</li>";            break;        case "Help2":            text = "[Page 2 of 2] Commands you can use: <li>North One More</li><li>East Once More</li><li>West Once More</li><li>South Once More</li><li>East Again</li><li>West Again</li>";            break;       case "South":            text = "You went to the city; street 2. Do you want to explore?";            break;    }}</script>現(xiàn)在說我想補(bǔ)充一下我對(duì)南方的看法。如您所見,有一個(gè)問題(請(qǐng)注意:在此之后還有其他情況)。我想添加類似“是”或“否”之類的內(nèi)容,如果我輸入“是”,它會(huì)給我類似“你玩得開心”之類的內(nèi)容。也不給我,“你決定不去探索南街 2 號(hào)。有沒有做這樣的事情?我嘗試開始另一個(gè)案例,但沒有成功。案例中的 if 和 else if 語句也不做。任何反饋將不勝感激。
查看完整描述

2 回答

?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

聽起來好像text正在被插入到 DOM 中,并且用戶必須在邏輯繼續(xù)之前輸入另一個(gè)輸入。這種附加值的輸入是異步的。表達(dá)鏈?zhǔn)疆惒酱a流的最清晰方法是使用async/ await:您可以創(chuàng)建一個(gè)返回 Promise 的函數(shù),該 Promise 在用戶按下按鈕時(shí)解析。每當(dāng)你想實(shí)現(xiàn)詢問用戶的邏輯時(shí),調(diào)用它和awaitPromise。


switch非常丑陋和冗長,考慮使用普通的if/else語句。


還要考慮小寫輸入文本,它會(huì)讓用戶更輕松。


避免內(nèi)聯(lián)處理程序也很好,它們有太多問題。改為使用 Javascript 附加偵聽器。


const button = document.querySelector('button');

const input = document.querySelector('input');

const nextInput = () => new Promise((resolve) => {

  button.onclick = () => {

    resolve(input.value.toLowerCase());

  };

});


const summary = document.querySelector('div');

const display = text => summary.innerHTML = text;

const main = async () => {

  const textInput = await nextInput();

  if (textInput === 'help1') {

    display("[Page 1 of 2. Type 'Help2' for page 2] Commands you can use: <ul><li>South</li><li>North</li><li>East</li><li>West</li><li>North Again</li><li>South again</li>");

  } else if (textInput === 'help2') {

    display("[Page 2 of 2] Commands you can use: <li>North One More</li><li>East Once More</li><li>West Once More</li><li>South Once More</li><li>East Again</li><li>West Again</li>");

  } else if (textInput === 'south') {

    display("You went to the city; street 2. Do you want to explore?");

    const exploring = await nextInput();

    if (exploring === 'yes') {

      display('Exploring');

    } else {

      display('Not exploring');

    }

  }

  // go back to beginning state

  main();

};


main();

<input type="text" placeholder="Type 'Help1' for actions">

<button>Confirm</button>

<div></div>


查看完整回答
反對(duì) 回復(fù) 2022-06-16
?
郎朗坤

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以嵌套另一個(gè)switch...case. 您只需要確保為答案分配了一個(gè)值,或者使用諸如 window.prompt 之類的東西來獲得答案。


例如:


switch (textInput) {

  case "Help1":

    text = "[Page 1 of 2. Type 'Help2' for page 2] Commands you can use: <ul><li>South</li><li>North</li><li>East</li><li>West</li><li>North Again</li><li>South again</li>""

    break;

  case "Help2":

    text = "[Page 2 of 2] Commands you can use: <li>North One More</li><li>East Once More</li><li>West Once More</li><li>South Once More</li><li>East Again</li><li>West Again</li>";

    break;

  case "South":

    switch(window.prompt("Do you want to explore?")){

      case "yes":

        console.log("user wants to explore")

        break;

      case "no":

        console.log("user does not want to explor")

        break;

    }

  break

}


查看完整回答
反對(duì) 回復(fù) 2022-06-16
  • 2 回答
  • 0 關(guān)注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)