怎么把随机数赋值到文本框里
java代码是选取不重复随机数,然后点击结束时将随机说显示在文本框里,我这样写的有错误啊
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test2.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var c=0
var t
function start()
{
document.getElementById('txt1').value=Math.round(Math.random() * (500- 1) + 1);
document.getElementById('txt2').value=Math.round(Math.random() * (500- 1) + 1);
t=setTimeout("start()",60)
}
function over(){
clearTimeout(t);
document.getElementById('txt1').value=<%=r[0]%>//这样写有错误啊 。。。
}
</script>
</head>
<body>
<%
Random C=new Random();
int r[]=new int[100];
for(int i=0;i<20;i++)
{
r[i]=C.nextInt(500);
for(int a=0;a<i;a++)
{
if(i==0)
{
break;
}
if(r[i]==r[a])
{
i--;
break;
}
}
}
for(int u=0;u<r.length;u++)
System.out.print(r[u]+",");
%>
<input type="text" id="txt1" value="" />
<input type="text" id="txt2" value="" />
<input type="button" onclick="start()" value="开始"/>
<input type="button" onclick="over()" value="结束"/>
</body>
</html>
<%
Random C=new Random();
int r[]=new int[100];
for(int i=0;i<20;i++)
{
r[i]=C.nextInt(500);
for(int a=0;a<i;a++)
{
if(i==0)
{
break;
}
if(r[i]==r[a])
{
i--;
break;
}
}
}
for(int u=0;u<r.length;u++)
System.out.print(r[u]+",");
%>
<script type="text/javascript">
var c=0
var t
function start()
{
document.getElementById('txt1').value=Math.round(Math.random() * (500- 1) + 1);
document.getElementById('txt2').value=Math.round(Math.random() * (500- 1) + 1);
t=setTimeout("start()",60)
}
function over(){
clearTimeout(t);
document.getElementById('txt1').value=<%=r[0]%>//这样写有错误啊 。。。
}
</script>
</head>
<body>
<input type="text" id="txt1" value="" />
<input type="text" id="txt2" value="" />
<input type="button" onclick="start()" value="开始"/>
<input type="button" onclick="over()" value="结束"/>
function start()
{
var txt = document.getElementById('txt1').value;
if(!txt==""){
txt ="";
}else{
document.getElementById('txt1').value=Math.round(Math.random() * (500- 1) + 1);
document.getElementById('txt2').value=Math.round(Math.random() * (500- 1) + 1);
t=setTimeout("start()",60);
}
}
<script type="text/javascript">这样就行了
var c=0;
var t;
function start()
{
document.getElementById('txt1').value=Math.round(Math.random() * (500- 1) + 1);
document.getElementById('txt2').value=Math.round(Math.random() * (500- 1) + 1);
t=setTimeout("start()",60);
}
function over(){
clearTimeout(t);
}
</script>