41 lines
949 B
JavaScript
41 lines
949 B
JavaScript
const readline = require("readline");
|
||
|
||
const rl = readline.createInterface(
|
||
process.stdin, process.stdout
|
||
);
|
||
let student_num = 10;
|
||
let grades = new Array(student_num); // 类似于C/C++中数组的数据结构,空间复杂度O(n)
|
||
|
||
function input(remain_times) {
|
||
if (remain_times <= 0) {
|
||
rl.close();
|
||
return;
|
||
}
|
||
rl.question("", (answer) => {
|
||
grades[student_num - remain_times] = parseFloat(answer); // 暂时不考虑输入错误
|
||
input(remain_times - 1);
|
||
});
|
||
}
|
||
rl.on("close", () => {
|
||
let sum = grades.reduce((sum, current) => {
|
||
return sum + current;
|
||
}, 0); // 时间复杂度O(n)
|
||
let average = sum / student_num;
|
||
console.log("平均分为:", average);
|
||
});
|
||
rl.write("输入"+student_num+"名学生的成绩,一个一行:\n");
|
||
input(student_num);
|
||
|
||
// 输入10名学生的成绩,一个一行:
|
||
// 1
|
||
// 2
|
||
// 3
|
||
// 4
|
||
// 5
|
||
// 6
|
||
// 7
|
||
// 8
|
||
// 9
|
||
// 10
|
||
// 平均分为: 5.5
|