Guess the number game note two points, 1. The random number and the guessing number cannot be placed on the same page, otherwise the random number will not stop appearing, guess the number cannot be equal.
2. Conversion of data types to each other. Wrapper class integer and int are used, the former is the class, the latter is the basic data type
cai.jsp
?
1 2 3 4 5 6 7 8 9 |
<body> 去猜数字----<a href=
"b.jsp"
>guess</a>
<%
int a=(
int
)(Math.random()*
100
);
session.setAttribute(
"number"
,a);
%>
<%=
"随机数为那:"
+session.getAttribute(
"number"
) %> </body>
|
b.jsp
?
1 2 3 4 5 6 |
<body> <form action= "da.jsp" > <input type= "text" name= "guess" /><br/> <input type= "submit" value= "提交" /> </form> |
da.jsp
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<body>
<%
Integer str1=(Integer)session.getAttribute(
"number"
);
String str2=request.getParameter(
"guess"
);
int num2=Integer.parseInt(str2);
int num1=Integer.valueOf(str1);
if
(num1==num2){
out.print(
"您好,猜对了,再玩一次,<a href=‘cai.jsp‘>guess</a>"
);
}
else if
(num1>num2){
out.print(
"您好,猜小了,再猜一次,<a href=‘b.jsp‘>guess</a>"
);
}
else if
(num1<num2){
out.print(
"您好,猜大了,再猜一次,<a href=‘b.jsp‘>guess</a>"
);
}
%>
|