多行输入输出
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let lines=[]
rl.on('line', function (line) {
lines.push(line);
if(lines.length=='xxx'){
rl.close()
}
}).on('close',() => {
//for循环输出
})
例题:输入一个数字,根据数字大小输入对应行数的两个值,要求逐行输出两个值之和:
以下为node环境下的输入输出,多行输入输出可作参考:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let lines=[]
rl.on('line', function (line) {
lines.push(line);
const num=parseInt(lines[0]);
if(lines.length==num+1){
rl.close()
}
}).on('close',() => {
for(let i=1;i<lines.length;i++){
let group=lines[i].split(' ').map(n=>parseInt(n));
console.log(group[0]+group[1]);
}
})
单行:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (line) {
const group=line.split('');
console.log(parseInt(group[0]+group[1])
})