当前位置:网站首页>JS uses readLine to realize terminal input data
JS uses readLine to realize terminal input data
2022-07-26 18:57:00 【*neverGiveUp*】
Preface : A while ago JS Brush problem , Encounter manual input data , I lost it , I didn't see it Js Code of manually entered data , So that it has been stuck , After several days, I met again , It can't be solved , Once or twice , So I looked up all kinds of information , But I feel that the information I'm looking for is not satisfactory , But I still learned something , I see. In Node Environmental use readline Realize single line input , But I haven't found a method of multi line input . But it can be realized by changing the environment , hold Node Environment change V8 Environmental Science . But I'm right about V8 Not really , I don't know how to replace , So don't talk too much here .
One 、 What is? readline
readline yes Node.js A encapsulated module that implements standard input and output in , Through this module, we can read the data stream line by line . Use require(“readline”) You can reference modules .
Two 、readline Use
# install
npm i readline
//1. introduce readline modular
const readline = require("readline")
//2. establish readline example
const r1 = readline.createInterface({
input:process.stdin,// The standard input
output:process.stdout// standard output
})
//3. call readline Related interface methods , Realize input and output
//str What is stored in is the data we input from the terminal
r1.question(" Please input data :",function(str){
console.log(" The data you entered is :",str);
// End output , This method must be called , otherwise readline Will be waiting for you to enter
r1.close()
})
//4. monitor readline Related events
r1.on("close",function(){
// sign out
process.exit(0);
})
Operation mode :
# The input terminal
node Js file
Running results :
give an example : Manual input , Find the sum of two numbers
const readline= require('readline')
const r1 = readline.createInterface({
input:process.stdin,
output:process.stdout
})
r1.question(' Please input data :', function(sum) {
let arr = sum.split(' ')
var all = 0;
for(let i=0 ;i<arr.length; i++){
all+= Number(arr[i])
}
console.log(` The result of the addition is :${
all}`)
r1.close()
})
r1.on("close",function(){
process.exit(0);
})
Running results :
Expand
V8 Multi line input in environment
while(line=readline()){
var lines = line.split(' ');
var a = parseInt(lines[0]);
var b = parseInt(lines[1]);
print(a+b);
}
Related information
https://blog.csdn.net/rraxx/article/details/115051515?ops_request_misc=&request_id=&biz_id=102&utm_term=JS%20read_line&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-4-115051515.142v33pc_search_result_control_group,185v2control&spm=1018.2226.3001.4187
https://www.nowcoder.com/discuss/276
边栏推荐
- Flask 封装七牛云
- The pit of mpc5744p reports an error, RTOS cannot be started, and there is a clock source problem
- 开发winform中遇到的一些问题汇总(持续跟新)
- OpenSSF 基金会总经理 Brian Behlendorf :预计 2026 年将有 4.2 亿个开源
- Redis core principles
- ECS MySQL prompt error
- CoVOS:无需解码!利用压缩视频比特流的运动矢量和残差进行半监督的VOS加速(CVPR 2022)...
- Have you ever encountered a deadlock problem in MySQL? How did you solve it?
- Linked list - reverse linked list
- 我酷故我在
猜你喜欢

【考研词汇训练营】Day 14 —— panini,predict,access,apologize,sense,transport,aggregation

Pyqt5 rapid development and practice 3.5 menu bar and toolbar

2022年焊工(初级)操作证考试题库及模拟考试

MySQL练习题初级45题(统一表)

SSM integration configuration

ALV screen input option learning

JS使用readline来实现终端输入数据

CTO will teach you: how to take over his project when a technician suddenly leaves

Learn UML system modeling from me

MySQL数据库命令大全
随机推荐
Real passwords do not match during synchronization
2022年化工自动化控制仪表考题模拟考试平台操作
mpc5744p的pit报错, RTOS无法启动, 时钟源问题
Accused of excessive patent licensing fees! The U.S. Court ruled that Qualcomm violated the antitrust law: Qualcomm's share price fell 10.86%!
Concentrate, heart to heart! The Chinese funded mobile phone Enterprises Association (CMA) of India is officially operational!
更安全、更健康、无续航焦虑,魏牌拿铁DHT-PHEV来了
NFT digital collection development: digital collections help enterprise development
基础模块及算例#pytorch学习
【MySQL从入门到精通】【高级篇】(八)聚簇索引&非聚簇索引&联合索引
The first ABAP ALV reporter construction process
Module 8 job message data MySQL table design
JS刷题计划——链表
SSM整合-功能模块和接口测试
Flask 封装七牛云
How the test team conducts QA specification
The principle of database index, why use b+ tree, why not binary tree?
Development of NFT digital collection system: Shanxi first released digital collections of ancient buildings on "China Tourism Day"
网络协议:TCP/IP协议
File upload and download test point
Linked list - the first common node of two linked lists