1 回答
TA贡献1828条经验 获得超3个赞
While 循环进入无限循环,因为传入的值始终为 true。根据您的要求,我建议使用递归。我在下面粘贴工作代码。让我知道它是否有效。
const math = require('./math');
const prompt = require('prompt');
prompt.start();
console.log("\n1.Add\n2.Substract\n3.Divide\n4.Multiply\n5.Exit\nEnter your choice (1-5):\n");
takeInputs()
function takeInputs () {
prompt.get([{
name: 'firstNumber',
type: 'number'
}, {
name: 'secondNumber',
type: 'number'
}], function (err, result) {
if (err) { onErr(err); }
prompt.get({
name: 'choice',
type: 'number'
}, function (err, data) {
if (err) { onErr(err) }
switch (data.choice) {
case 1:
console.log("sum is " + math.add(result.firstNumber, result.secondNumber));
return takeInputs()
case 2:
console.log(`Substraction is ${math.sub(result.firstNumber, result.secondNumber)}`);
return takeInputs()
case 3:
console.log(`Division is ${math.divide(result.firstNumber, result.secondNumber)}`);
return takeInputs()
case 4:
console.log(`multiplication is ${math.multiply(result.firstNumber, result.secondNumber)}`)
return takeInputs()
case 5: process.exit(0);
default: console.log("You had entered invalid choice.");
return takeInputs()
}
})
});
}
function onErr (err) {
console.log(err);
return 1
}
添加回答
举报