开发者

javascript and live connect

开发者 https://www.devze.com 2023-02-27 03:10 出处:网络
i wrote a simple html file that prints 10 random numbers using javascript. javascript object in turn uses java.util.Random class (live connect) to output the result! but i am not getting exact output

i wrote a simple html file that prints 10 random numbers using javascript. javascript object in turn uses java.util.Random class (live connect) to output the result! but i am not getting exact output when using with functions and events like onLoad or onclick!

<html>
<body onLoad="hello()">
<script language="javascript">
function hello()
{
var i=0;
for(;i<10;i++)
{
var j=new java.util.Random(i);
document.writeln(j);
}
}
</script></body></html>

And i can get output if we dont use function!

<html>
<body >
<script language="javascript">
var i=0;
for(;i<10;i++)
{
var j=new java.util.Random(i);
document.writeln(j);
}
</script></body></html>
开发者_如何学JAVA

can anybody help me with this bug!


Use this:

var j=Math.random()*i; instead of var j=new java.util.Random(i);


Perhaps this is the correct syntax?

for(;i<10;i++){
  Random r =  new Random();
  int j =  r.nextInt(i)
  document.writeln(j);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消