有没有办法在switch语句后重复console.log



console.log将选项记录到用户

console.log("Menu" + "n" + "1. Read File" + "n" "2. Exit")
var userInput = readlineSync.question('Option:');

这将记录;菜单1.读取文件2.退出选项:

当用户选择2 时,我想重复EXPECT

我将输入设置为int以使用开关

let input = parseInt(userInput)
switch (input) {
case 0:
text = "Off";
break;
case 1:
console.log('You have selected to read csv file')
readFile();
break;
case 2:
console.log("Quitting program")
break;
default:
text = "No value found";
}
function readFile(){
var info = fs.readFileSync("FILE", 'utf8', function (err, data) {
if (err) throw err; 
console.log("display current items: n"+data); 
})
console.log(info)
}

在switch语句和函数之后,我如何让控制台记录菜单并询问用户的输入?

您将需要一个循环结构,如while

function readFile(){
...
}
let input;
while (input != 2) {
console.log("Menu" + "n" + "1. Read File" + "n" +
"2. Exit")
var userInput = readlineSync.question('Option:');
input = parseInt(userInput)
switch (input) {
case 0:
text = "Off";
break;
case 1:
console.log('You have selected to read csv file')
readFile();
break;
case 2:
console.log("Quitting program")
break;
default:
text = "No value found";
}
}

最新更新