简体   繁体   English

我们如何在 java 脚本控制台中获取用户输入

[英]How do we get user input in java script console

In Python I used input function to get user input but in javascript I am trying to use prompt() which I seen in goolgle search but it is not working.在 Python 中,我使用输入 function 来获取用户输入,但在 javascript 中,我尝试使用在 goolgle 搜索中看到的 prompt(),但它不起作用。 I am using visual studio code editor.我正在使用 Visual Studio 代码编辑器。 the code is something like that,代码是这样的,

let name = prompt('what is your name')
console.log(name + 'is your name')

If you are using NodeJS, to prompt for user input, you can use the readline module.如果您使用的是 NodeJS,要提示用户输入,您可以使用readline模块。

Here is an example of how you can prompt the user for their name and then greet them:下面是一个如何提示用户输入他们的名字然后问候他们的例子:

const readline = require('readline');

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

rl.question('What is your name? ', (name) => {
  console.log(`Hello, ${name}!`);
  rl.close();
});

More info: https://nodejs.org/api/readline.html .更多信息: https://nodejs.org/api/readline.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM