犀牛的控制台輸入功能? (console input function for rhino?)


問題描述

犀牛的控制台輸入功能? (console input function for rhino?)

How do I accept a variable from the console with javascript in Rhino? anything similar to cin or scanf?


參考解法

方法 1:

Here's two lines that'll do what you want:

var stdin = new BufferedReader( new InputStreamReader(System['in']) )
var aLine = stdin.readLine();

方法 2:

In Rhino you have to remember to import Java packages before you can use them. Also, Java String differs from JavaScript native String, so you may want to cast it.

Here is a quick-and-dirty readln() that works the same in both SpiderMonkey and Rhino:

 var readln = (typeof readline === 'function') ? (readline) : (function() {
     importPackage(java.io);
     importPackage(java.lang);
     var stdin = new BufferedReader(new InputStreamReader(System['in']));

     return function() {
         return String(stdin.readLine());  // Read line, 
     };                                    // force to JavaScript String
 }());

方法 3:

I hope this will help you:

Simple function that reads a line from console

function readline()
{
    var ist = new java.io.InputStreamReader(java.lang.System.in); 
    var bre = new java.io.BufferedReader(ist); 
    var line = bre.readLine();
    return line;
}
print("Name? ");
var name=readline();
print("Your name is: "+name);

方法 4:

Just use the Java class library. I think this will work:

var stdin = java.lang.System.in;
var line = stdin.readLine();

At that point it's easy to convert the line to whatever type you like, or to break it into pieces using a RegExp.

This could garble Unicode input, but I'm not sure there's a good way around that, cross-platform.

方法 5:

var ins = java.lang.System.in;
var newLine = java.lang.System.getProperty("line.separator");
var is = new java.io.InputStreamReader(ins);
var sb=new java.lang.StringBuilder();
var br = new java.io.BufferedReader(is);
var line = br.readLine();
while(line != null) {
    sb.append(line);
    sb.append(newLine);
    line = br.readLine();
}
var stdin = ""+sb.toString();//java string != javascript string
console.log("stdin:"+stdin);

(by mihsatheB TTeroHarmonixJason OrendorffIlya Kharlamov)

參考文件

  1. console input function for rhino? (CC BY-SA 3.0/4.0)

#Console #javascript #rhino






相關問題

grails 日誌消息未顯示在 STS 3.0 控制台上 (grails log messages not displaying on STS 3.0 console)

在 AppFog 上,如何在終端(node.js)上查看控制台日誌 (on AppFog, how to see the console logs on the terminal (node.js))

控制台應用程序中的多個參數未正確解析 (Multiple args in Console Application not parsing correctly)

WPF 應用程序沒有輸出到控制台? (No output to console from a WPF application?)

用 .NET 4.5 編寫的 Windows 服務中的 Console.Out 和 Console.Error 競爭條件錯誤 (Console.Out and Console.Error race condition error in a Windows service written in .NET 4.5)

Visual Basic 2013 - 控制台輸入令牌? (Visual Basic 2013 - Console Input Tokens?)

帶有 stringbuilder 日曆表的控制台日曆 (Console Calendar with stringbuilder calendar sheet)

增加和減少混淆 (Incrementing and Decrementing Confusion)

有沒有辦法在標準 C++ 中直接從鍵盤讀取輸入? (Is there a way to read input directly from the keyboard in standard C++?)

是否可以在方案中編寫控制台應用程序? (Is it possible to write console applications in scheme?)

犀牛的控制台輸入功能? (console input function for rhino?)

輸出中的 PowerShell 空白 (PowerShell gaps in output)







留言討論