[英]How can I write a simple JScript input/output program?
I'm planning on using JavaScript to enter an informatics competition (BIO) tomorrow.我计划明天使用 JavaScript 参加信息学竞赛 (BIO)。 However, I can't rely on the examiner having a browser with a decent JavaScript engine, so I was hoping to use Microsoft's JScript instead.
但是,我不能指望考官拥有带有不错 JavaScript 引擎的浏览器,所以我希望改用 Microsoft 的 JScript。
However, the documentation is, quite frankly, crap.然而,坦率地说,文档是垃圾。 Can someone post some example code that reads in a line of text, calls
foo(string)
on it, and echos the output to the command line?有人可以发布一些示例代码,读取一行文本,在其上调用
foo(string)
并将输出回显到命令行吗?
Similarly, how do I actually run it?同样,我如何实际运行它? Will
wscript.exe PATH_TO_JS_FILE
do the trick? wscript.exe PATH_TO_JS_FILE
会wscript.exe PATH_TO_JS_FILE
吗?
If you're using the command-line, I'd execute the script using CSCRIPT.EXE
.如果您使用命令行,我会使用
CSCRIPT.EXE
执行脚本。 ie: CSCRIPT.EXE myscript.js
This is because WScript.Echo
from WSCRIPT
will create a dialog box and from CSCRIPT
outputs a line to the console.即:
CSCRIPT.EXE myscript.js
这是因为来自WSCRIPT
WScript.Echo
将创建一个对话框,并从CSCRIPT
输出一行到控制台。 Run this in a command window (CMD).在命令窗口 (CMD) 中运行它。
Reading a line from console into variable:从控制台读取一行到变量中:
var x = WScript.StdIn.ReadLine();
Where StdIn
is a TextStream object .其中
StdIn
是一个TextStream 对象。 There is also an StdOut
which can be used in place of WScript.Echo()
...还有一个
StdOut
可以用来代替WScript.Echo()
...
Writing the output of foo(x)
to console: (must run under CSCRIPT
)将
foo(x)
的输出写入控制台:(必须在CSCRIPT
下运行)
WScript.Echo(foo(x));
You can use the WScript
object to determine which engine you are running under, there's a question/answer for that (VBScript, but uses the same objects under JScript) here .您可以使用
WScript
对象来确定你是下运行,其发动机,有一个问题/答案为(VBScript中,但在JScript中使用相同的对象)这里。
If you want to be sure your program only runs in the command line, you may use the WScript.StdIn
and WScript.StdOut
objects/properties:如果您想确保您的程序只在命令行中运行,您可以使用
WScript.StdIn
和WScript.StdOut
对象/属性:
var myString = WScript.StdIn.ReadLine();
WScript.StdOut.WriteLine(myString);
and run it with cscript.exe
.并使用
cscript.exe
运行它。 But if you want it to be a GUI program, it is a bit more difficult considering that JScript doesn't have a native InputBox
function like VBScript
.但是如果你想让它成为一个 GUI 程序,考虑到 JScript 没有像
VBScript
这样的原生InputBox
函数,这就有点困难了。 However, as described here we may use Windows Script Host (WSH) .但是,如此处所述,我们可能会使用Windows Script Host (WSH) 。 Create a
.wsf
file:创建一个
.wsf
文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<job id="testJScriptInputBox">
<script language="VBScript">
<![CDATA[
Function WSHInputBox(Message, Title, Value)
WSHInputBox = InputBox(Message, Title, Value)
End Function
]]>
</script>
<script language="JScript">
<![CDATA[
var vbOKOnly = 0; // Constants for Popup
var vbInformation = 64;
var title = "InputBox function for JScript";
var prompt = "Enter a string: ";
var WshShell = WScript.CreateObject("WScript.Shell");
var result = WSHInputBox(prompt, title, "New York");
if (result != null)
{ // Cancel wasn't clicked, so get input.
var intDoIt = WshShell.Popup(result,
0,
"Result",
vbOKOnly + vbInformation);
}
else
{ // Cancel button was clicked.
var intDoIt = WshShell.Popup("Sorry, no input",
0,
"Result",
vbOKOnly + vbInformation);
}
]]>
</script>
</job>
and run it with either cscript.exe
or wscript.exe
.并使用
cscript.exe
或wscript.exe
运行它。 Alternatively, you could also use HTML Application (HTA) to create more elaborate GUIs.或者,您也可以使用HTML 应用程序 (HTA)来创建更精细的 GUI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.