繁体   English   中英

当用户输入“否”时,如何结束此过程

[英]How can I make this process end when user inputs “no”

我试图通过CLI向用户提出一些问题,我遇到一个小问题,当下面响应功能中的问题回答为“否”时,所选的产品功能仍会继续并打印出味精。

我意识到这是因为到目前为止,我在其他情况下都获得else if“否”响应,但是我不确定该将其放置在什么位置,因为当我之前将其放置在代码上方时,它也不起作用。

readline = require('readline-sync');

var rl = require('readline');

var prompts = rl.createInterface(process.stdin, process.stdout);

prompts.question("Hi, are you ready to order? (Yes/No) ", function(response) { //This will be the initial display until the customer engages.

var msg = "";

if (response === "Yes") {

    msg = "Great, first lets check your balance";

    console.log(msg);
    process.exit();

}
});

prompts.question("Your balance is currently £0.00, would you like to add credit? (Yes/No) ", function(credit) {

var msg ="";

if(credit === "Yes") {

    var addCredit = "OK, please enter the amount you would like to add. (Format £0.00)";

    bill.question("")

}
else if(credit === "No") {
    msg = "No problem, you will now be taken back to the main menu.";
}

if(addCredit = ("OK, please enter the amount you would like to add. (Format £0.00)")) {

    prompts.question("")

}
})

您必须存储从提示方法返回的值。

采用:

var answer = prompt("Hi, are you ready to order? Type Yes or No");

//编辑:

我看不到您为什么要向该函数传递变量(答案)

我会这样写:

function questions() {
    var answer = prompt("Hi, are you ready to order? Type Yes or No");
    if (answer === "Yes") {
        console.log(questions)
    }
}

尝试这个。 问题在于您嵌套条件的方式。 对于第二个提示,我建议您使用switch语句。 只是为了使您的使用恢复到默认消息。

function getCustomerOrder() {
  prompts.question("Hi, are you ready to order? (Yes/No)", function(response) {
    var msg = "";

    if (response === "Yes") {
      msg = "Great, lets get into it!";
      console.log(msg);
      getPurchase();

    }
    else if (response === "No") {
      msg = "No problem, take your time.";
      console.log(msg);
      process.exit(1);
    }
  });
};


function getPurchase() {
  prompts.question("What would you like to purchase? (Please type exact name of Product) ", function(productchosen) {
    var msg = "";

    if (productchosen === "Orange Lucozade") {
      msg = "Orange Lucozade, good choice! That will be £1.00";
    }
    else if (productchosen === "Original Lucozade") {
      msg = "Original Lucozade, good choice! That will be £1.00";
    }
    else if (productchosen === "Apple Lucozade") {
      msg = "Apple Lucozade, good choice! That will be £1.00";
    }

    console.log(msg);
  });
};

// getCustomerOrder();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM