⚡ 编程实验室🏗️ HTML🎨 CSS⚡ JavaScript🐍 Python🗄️ SQL☕ Java⚛️ React💚 Vue🟢 Node.js⚙️ C语言🐘 PHP🐹 Go🔷 TypeScript🐬 MySQL🔧 C++🎯 C#🦀 Rust🅱️ Bootstrap💡 jQuery🎸 Django🍃 MongoDB👗 Sass🎪 Kotlin📊 R语言📋 XML📊 Excel🐘 PostgreSQL🐳 Docker🅰️ Angular🎮 游戏🏠 网站首页

Node.js readline — 命令行交互

创建命令行问答界面,读取用户输入 · 难度:入门 · +15XP

readline 模块

readline 让你在终端中逐行读取用户输入,适合做命令行工具和交互式脚本。

基本用法

const readline = require("readline");

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

rl.question("你叫什么名字?", (name) => { console.log(你好,${name}!); rl.close(); });

多轮问答

const questions = ["你的名字?", "你的年龄?", "你的城市?"];
const answers = [];
let i = 0;

function ask() { if (i >= questions.length) { console.log(" 你的信息:", answers.join(", ")); rl.close(); return; } rl.question(questions[i], (ans) => { answers.push(ans); i++; ask(); }); } ask();

Ctrl+Enter
🚀 升级VIP
解锁全部课程+AI助手

🏆 学习排行

加载中...

📊 统计

📖 135 篇
0 完成
🔥 0