himorogiの日記

主にプログラミングに関することなど。少々ハード(電子工作)についても。

Rhino(Javascript)には標準入出力がないので、Java の標準入出力を使う

※ Sun JavaSDK の jrunscript を使わない場合

Java のクラスライブラリを読み込んで object を new

importPackage(java.io);
var rd = new BufferedReader(new InputStreamReader(java.lang.System['in']));
var wr = new OutputStreamWriter(java.lang.System['out']);

□入力のテスト

>print(rd.readLine());
harahirehoro
harahirehoro
>

※ ここでは入力した行をそのまま print してるだけなので、あんまり意味は無い…

□ 出力のテスト

>wr.write("hello java world");
>wr.flush();
hello java world>

※ 出力文字列に改行が入ってなかったので prompt が出力文の直後に登場している
※ flush() 必須

Rhino 版 Lotos (file name:kuji.js)

importPackage(java.io);
var rd = new BufferedReader(new InputStreamReader(java.lang.System['in']));
var wr = new OutputStreamWriter(java.lang.System['out']);

var lotos =function (depth){
  var ceil, mask=[0,0], coll =[];

  function getNum(){ return(function(x){ return x.substr(x.length-2,2) })("00"+ Math.ceil(Math.random()*ceil)); };
  isnotUniq= function(x) { return 0!=(mask[Math.floor(x/32)] &  1<<(x%32)); };
  pushNum= function(x) { mask[Math.floor(x/32)] |= 1<<(x%32); coll.push(x); }

  ceil=(function(x){for(i=c=0;i<x; c+= ++i);return c})(depth)*2+1;
  pushNum(getNum());
  while( coll.length<depth ){ pushNum((function(){ while(isnotUniq(x=getNum())); return x; })()); }
  return coll;
};
wr.write("5:minilot\n6:loto6\n");wr.flush();
sx=rd.readLine();
if(sx<5 || sx>6){ wr.write("bad arguments\n"); }else{ wr.write(lotos(sx).sort().toString()); }
wr.write("\n");wr.flush();