<!-- guessCharExample.jsp -->
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>猜字母游戏</title>
</head>
<body>
以下。我们一起来玩一个游戏:猜字母游戏<br>
游戏规则:电脑会随机自己主动生成一个字母。请你猜出这个字母是什么。字母忽略大写和小写。<br>
<% String charString=new String("abcdefghijklmnopqrstuvwxyz");
int charNumber=((int)(Math.random()*100)+1)%26-1;
Character TempCharacter=new Character(charString.charAt(charNumber));
session.setAttribute("TempCharacter",TempCharacter);
%>
<br>
<p>输入你所猜的字母:</p>
<form action="guessResultExample.jsp" method="post" name=form>
<input type="text" name="guesschar">
<input type="submit" value="提交" name="submit">
</form>
</body>
</html>
<!-- guessResultExample.jsp -->
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>猜字母游戏</title>
</head>
<body>
<%
String tempString=request.getParameter("guesschar");
String TempCharacter=session.getAttribute("TempCharacter").toString();
if(tempString!=null)
{
if(TempCharacter.equalsIgnoreCase(tempString))
{
out.println("恭喜你。你猜对了!");
}
else
{
out.println("你猜错了,加油哦!");
}
}
%>
<br>
<p>输入你所猜的字母:</p>
<form action="guessResultExample.jsp" method="post" name=form>
<input type="text" name="guesschar">
<input type="submit" value="提交" name="submit">
</form>
<a href="guessCharExample.jsp">又一次開始游戏</a>
</body>
</html>