Javascript条件语句



我几天来一直在努力让这段代码在Javascript的Conditional语句中正常工作。我只是迷路了,无法正常工作。它需要满足以下条件,使用提示从用户获得输入

  1. 如果用户试图在不将杯子放在托盘,显示"请将杯子放在托盘上。">
  2. 如果是自定义可再填充杯,则显示"自定义可再灌注cup:请自己选择。">
  3. 如果是惯例不可重复充装的杯子,并且是第一次使用,显示"定制不可重复灌装的杯子:您还有一杯;请制作您的选择。">
  4. 如果它是定制的不可再填充的杯子,并且它不是第一个使用时,显示"自定义不可再填充杯子:您没有剩余填料;请离开。">
  5. 如果不是任一自定义杯子,则显示"This is an无效杯子;请离开">

它问:你们把杯子放在托盘上了吗?如果我说:是的,上面写着请选择。若我拒绝:上面写着请把杯子放在托盘上。那么它问:你有refil杯子吗?如果我说:是的上面写着定制refil杯子结束如果我说:不它问你有无止回杯?如果我说:是的它问道:这是第一次使用吗?如果我说不上面写着"请零续费"结束如果我答应上面写着还剩一杯,请选择那么如果我拒绝不续杯和续杯上面写着无效的杯子请离开关闭

我现在遇到的问题是,当你对加油说不,然后对不加油说不时,需要说错杯子,请离开另一个问题是当你答应REFILL Cup它会显示正确的信息,上面写着请做出选择当你点击OK时,它不会关闭,它会继续询问你是否有NON-REPLAC杯

到目前为止我的代码:

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");
}

这是正确的代码

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.")
}

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

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.")
}

最新更新