有没有一种方法可以在for循环中使用readline?

时间:2020-06-30 16:14:03

标签: javascript for-loop readline

我正在尝试编写一个脚本,使其在输入时连续运行5次,例如输入“ 5”。因此,如果我将金额设为3,它应该问我3个问题,并记录3次不同的回答。

我已经尝试过了,但是无法正常工作。

var readline = require('readline');
var log = console.log;

var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});


rl.question("amounts to run", function(amounts){
var i;
for (i = 0; i < amounts; i++){
    rl.question("how is your day? ", function(answers){
        log(answers)
    })
}
});

rl.close();

提前谢谢!

1 个答案:

答案 0 :(得分:1)

Bergi,谢谢!我解决了。

const reader = require("readline-sync"); //npm install readline-syn
var log = console.log


let amount = reader.question("Amounts: ");
var i;
  for (i=0; i < amount; i++){
let answer = reader.question("How is your day? ")
log(answer)

};