有没有办法用 JavaScript 读取标准输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5396020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to read standard input with JavaScript?
提问by JohnAllen
I saw this for lots of other languages but not JavaScript.
我在许多其他语言中看到了这一点,但没有看到 JavaScript。
I'm trying to do problems like: this (codechef.com) and of course the programs need to be able to read standard in like C++ and other languages do.
我正在尝试做这样的问题:this (codechef.com),当然程序需要能够像 C++ 和其他语言一样阅读标准。
EDIT: Thanks for the answers. The primary reason I want this functionality is so I can answer the questions on CodeChef; Codechef sends multiple inputs to the files/programs that are the answers (and of course the programs have to respond in the required way for the answer to be correct).
编辑:感谢您的回答。我想要这个功能的主要原因是我可以回答 CodeChef 上的问题;Codechef 向作为答案的文件/程序发送多个输入(当然,程序必须以所需的方式响应才能使答案正确)。
采纳答案by Cameron
It depends on the environment that your JavaScript is executing in.
这取决于您的 JavaScript 正在执行的环境。
In the browser, there is no standard input (the browser isn't a console). The input would come generally from some textbox element in a form on the page.
在浏览器中,没有标准输入(浏览器不是控制台)。输入通常来自页面上表单中的某个文本框元素。
If you're using something like Rhino, then you can import the standard Java I/O classes and read from stdin that way (see the second exampleno longer on the wiki).
如果您使用的是Rhino 之类的东西,那么您可以导入标准 Java I/O 类并以这种方式从 stdin 读取(看第二个例子不再在维基上)。
回答by Kenn Costales
If you use node to act as an interpreter in the terminal, you can use this:
如果使用 node 在终端中充当解释器,则可以使用以下命令:
---- name.js ----
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(">>What's your name? ", function(answer) {
console.log("Hello " + answer);
rl.close();
});
----- terminal ----
node name.js
回答by TwiToiT
In some environments like interviewstreet's, they provide you with a function in which all the input is coming as an argument in form of a string.
在诸如interviewstreet 的某些环境中,它们为您提供了一个函数,其中所有输入都以字符串形式作为参数出现。
All of the input at once.
一次性输入所有内容。
Then you have to parse that input to get various tokens out of that string.
然后您必须解析该输入以从该字符串中获取各种标记。
After this you should be good to write the code further.
在此之后,您应该可以进一步编写代码。
回答by Matthew Flaschen
It's not in the ECMAScript (standardized version of JavaScript) standard library. However, some implementations of JavaScript do include it. For example, CommonJS, which is used by several out-of-the-browser JavaScript environments, has a system.stdinproperty. Rhinocan use Java's standard input classes.
它不在 ECMAScript(JavaScript 的标准化版本)标准库中。但是,JavaScript 的某些实现确实包含它。例如,被多个浏览器外的 JavaScript 环境使用的 CommonJS 有一个system.stdin属性。 Rhino可以使用 Java 的标准输入类。
If you're just trying to practice programming, you can use a textareaas a substitute for standard input.
如果您只是想练习编程,则可以使用textarea作为标准输入的替代品。
回答by Dinesh kumar Mohanty
Use just readline()
只使用readline()
var a = readline();
the value which you give input will be stored in variable a.
您输入的值将存储在变量 a 中。
readline(): Reads a single line of input from stdin, returning it to the caller. You can use this to create interactive shell programs in JavaScript.
readline():从 stdin 读取一行输入,将其返回给调用者。您可以使用它在 JavaScript 中创建交互式 shell 程序。