VS编辑器或Node js编辑器中的提示问题



当我想通过命令提示符vs代码或节点js编辑器在代码中接受用户输入时,编辑器说它没有定义。但据我所知,我们可以通过";提示"。

示例:-

function leapYear(year) {
if ((year %2 == 0) && (year %100 != 0) || (year %400 == 0)) {
console.log(year + " LeapYear");
}
else{
console.log(year +" Not a LeapYear");
}

}
const year = prompt('Enter a year:');

leapYear(year);

未捕获引用错误:未定义提示

是的,您可以从命令行获得用户输入(因此使用节点(,使用readline:

const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = "";
rl.question("input here:n", function (string) {
input = string;
console.log("input that was entered: " + input);
rl.close();

});

这比其他语言更麻烦。

更多信息:https://www.educative.io/edpresso/how-to-get-user-input-from-command-line-with-javascript

还有很多东西要读,所以这里是官方文档:

https://nodejs.org/api/readline.html

最新更新