首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript条件语句

Javascript条件语句
EN

Stack Overflow用户
提问于 2022-02-21 00:47:40
回答 1查看 64关注 0票数 -4

几天来,我一直试图让这些代码在条件语句的Javascript中正常工作。我只是迷路了,不能让它正常工作。它需要满足以下条件,使用提示符从用户获取输入

如果用户试图获取饮料而不将杯子放在托盘上,则显示“请将杯子放在托盘上”。如果是自定义可填充杯,则显示“自定义可填充杯:请选择”。如果是自定义不可再灌装杯,则显示“

  1. ”,如果这是第一次使用该杯,则显示“custom refillable杯:您还有一个剩余的填充杯;请选择。“
  2. ,如果它是自定义不可再填充杯,并且不是第一次使用它,则显示”自定义非填充杯:您有零填充剩余;请离开。“
  3. ,如果它不是自定义杯,则显示”这是一个无效杯;请离开。“

它问:你把杯子放在托盘上了吗?如果我说:是的,上面写着请选择。如果我说不:上面写着请把杯子放在托盘上。然后,它问:你有回收杯吗?如果我说:是的,它说是定制的回收杯,选择关闭如果我说:不,它问你有无续杯?如果我说:是的,它问:它是第一次使用吗?如果我说不,它会说请留下零重覆,关闭,如果我说是,它说一续,请选择,如果我说不,它说不,它说无效的杯子,请不要关闭。

我现在遇到的问题是,当你对续杯说不,然后对不续杯说不,它需要说错杯子,请离开;另一个问题是,当你对续杯说是时,它会显示正确的信息,当你点击if时,请做出选择,它不会关闭,它会继续,并询问你是否有无续杯。

到目前为止我的代码是:

代码语言:javascript
复制
let thecup = window.prompt("Did you place the cup on the tray?", "");   
if (thecup === "yes") {
    alert("Please choose your cup");
    } 
else {
    alert("Please put cup on the tray");
    }
        
//If it is the custom refillable cup, display “Custom Refillable cup: Please make your selection.”

let refillcup = window.prompt("Do you have a refillcup?");
if (refillcup === "yes") {
    alert("please make your selection")
    }
else {
    window.prompt("Do you have a Non-Refill?", "");
    }
        
let nonrefillcup = window.prompt("is it first time use?");
if (nonrefillcup === "yes") {
    alert("one refill remaining");
    } 
else {
    alert("Zero remaining refills");
    }

这是正确的代码

代码语言:javascript
复制
alert("Please put cup on the tray.")

let theCup = window.prompt("Did you place the cup on the tray? (type yes or no.)");
if (theCup === 'yes') {
    alert("Please choose your cup.")
}

let yourCup = window.prompt("Do you have a Refill cup or a NonRefill cup?");
if (yourCup === 'yes') {
    alert("Please choose your cup");

  let refillCup = window.prompt("Do you have a refill cup? (Answer must be yes or no.)");
  if (refillCup === 'yes') {
    alert("please make your selection");
  } else {
        let firstUse = window.prompt("Is it the first time use? (Answer must be yes or no.)");
    if (firstUse === 'yes') {
      alert("one refill remaining");
    } else {
      alert("You have zero refills remaining. Please leave.")
    }
  }
} else {
    alert("Wrong Cup. Please leave.")
}
EN

回答 1

Stack Overflow用户

发布于 2022-02-21 01:50:20

如果我正确理解了这个问题,我认为这是一个可能的解决办法:

代码语言:javascript
复制
alert("Please put cup on the tray.")

let theCup = window.prompt("Did you place the cup on the tray? (Answer must be yes.)");

if (theCup === 'yes') {
    alert("Please choose your cup.")

  let refillCup = window.prompt("Do you have a refill cup? (Answer must be yes or no.)");
  if (refillCup === 'yes') {
    alert("please make your selection");
  } else {
    let firstUse = window.prompt("Is it the first time use? (Answer must be yes or no.)");
    if (firstUse === 'yes') {
      alert("one refill remaining");
    } else {
      alert("You have zero refills remaining. Please leave.")
    }
  }
} else {
    alert("Your cup must be on the tray. Please leave.")
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71200098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档