如何编写一个简单的 JScript 输入/输出程序?

发布于 2024-10-07 12:59:26 字数 250 浏览 0 评论 0原文

我计划明天使用 JavaScript 参加信息学竞赛 (BIO)。然而,我不能依赖考官拥有一个带有像样的 JavaScript 引擎的浏览器,所以我希望使用 Microsoft 的 JScript。

然而,坦率地说,该文档是垃圾。有人可以发布一些示例代码来读取一行文本,对其调用 foo(string) 并将输出回显到命令行吗?

同样,我如何实际运行它? wscript.exe PATH_TO_JS_FILE 可以解决问题吗?

I'm planning on using JavaScript to enter an informatics competition (BIO) tomorrow. 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.

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?

Similarly, how do I actually run it? Will wscript.exe PATH_TO_JS_FILE do the trick?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2024-10-14 12:59:26

如果您使用命令行,我将使用 CSCRIPT.EXE 执行脚本。
即:CSCRIPT.EXE myscript.js
这是因为 WSCRIPT 中的 WScript.Echo 将创建一个对话框,并从 CSCRIPT 向控制台输出一行。在命令窗口 (CMD) 中运行此命令。

从控制台读取一行到变量中:

var x = WScript.StdIn.ReadLine();

其中 StdInTextStream 对象。还有一个 StdOut 可以用来代替 WScript.Echo()...

foo(x) 的输出写入控制台:(必须在 CSCRIPT 下运行)

WScript.Echo(foo(x));

您可以使用 WScript 对象来确定您在哪个引擎下运行,有一个问题/答案(VBScript,但使用JScript 下的相同对象)此处

If you're using the command-line, I'd execute the script using 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. Run this in a command window (CMD).

Reading a line from console into variable:

var x = WScript.StdIn.ReadLine();

Where StdIn is a TextStream object. There is also an StdOut which can be used in place of WScript.Echo()...

Writing the output of foo(x) to console: (must run under 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.

|煩躁 2024-10-14 12:59:26

如果您想确保您的程序仅在命令行中运行,您可以使用 WScript.StdInWScript.StdOut 对象/属性:

var myString = WScript.StdIn.ReadLine();
WScript.StdOut.WriteLine(myString);

并使用 运行它>cscript.exe。但如果你想让它成为一个 GUI 程序,考虑到 JScript 没有像 VBScript 那样的原生 InputBox 函数,那就有点困难了。但是,正如此处所述,我们可以使用Windows 脚本宿主 (WSH)。创建 .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>

并使用 cscript.exewscript.exe 运行它。或者,您也可以使用 HTML 应用程序 (HTA) 来创建更复杂的 GUI。

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:

var myString = WScript.StdIn.ReadLine();
WScript.StdOut.WriteLine(myString);

and run it with 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. However, as described here we may use Windows Script Host (WSH). Create a .wsf file:

<?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. Alternatively, you could also use HTML Application (HTA) to create more elaborate GUIs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文