题解 | 多组数据a+b III
多组数据a+b III
https://www.nowcoder.com/practice/7e094c0a3a9945b3bee8e1f3c9ea246a
const rl = require("readline").createInterface({ input: process.stdin }); function str_num_array(line) { let tokens = line.split(" "); let a = parseInt(tokens[0]); let b = parseInt(tokens[1]); return [a, b]; } rl.on("line", (line) => { if (str_num_array(line)[0] === 0 && str_num_array(line)[1] === 0) { rl.close(); } else console.log(str_num_array(line)[0] + str_num_array(line)[1]); }); //下列代码多输出结果0。即使立马按行读取关闭,也会执行玩块里的代码再结束。 // rl.on("line", (line) => { // if (str_num_array(line)[0] === 0 && str_num_array(line)[1] === 0) { // rl.close(); // } // console.log(str_num_array(line)[0] + str_num_array(line)[1]); // }); //改进版本:优化函数和函数调用。 // const rl = require("readline").createInterface({ input: process.stdin }); // function parseNumbers(line) { // const [a, b] = line.split(' ').map(Number); // return [a, b]; // } // rl.on("line", (line) => { // const [a, b] = parseNumbers(line); // if (a === 0 && b === 0) { // rl.close(); // } else { // console.log(a + b); // } // }); // rl.on("close", () => { // // });