课外天地 李树青学习天地Java程序语言课件 → 程序代码——基于JSP的猜数字游戏


  共有16180人关注过本帖树形打印复制链接

主题:程序代码——基于JSP的猜数字游戏

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——基于JSP的猜数字游戏  发帖心情 Post By:2007/5/11 6:11:10 [只看该作者]

用于生成随机数的CRandom.java文件为:

package mypro;

public class CRandom
{
private int initialNumber=0;
private int rand=0;

public CRandom()
{
}

public CRandom(int init)
{
  setInitialNumber(init);
}

public void setInitialNumber(int num)
{
  if(num<0)
   initialNumber=0;
  else
   initialNumber=num;
  generateRandom();
}

public void generateRandom()
{
  rand=(int)(Math.random()*initialNumber)+1;
}

public int getRandom()
{  
  return rand;
}
}

用于保存提示信息的CInfo.java文件为:

package mypro;

public class CInfo
{
private String info;

public void setInfo(String str)
{
  info=str;
}

public String getInfo()
{
  return info;
}
}

用于猜数字的主页exec.jsp文件为:

<%@page c%>
<html>
<head>
</head>
<body>
  <jsp:useBean id="cr" scope="session" class="mypro.CRandom"/>
  <jsp:useBean id="ci" scope="session" class="mypro.CInfo"/>
  <%
   if(session.isNew())
   {
    ci.setInfo("Hello!");
    cr.setInitialNumber(100);
    //out.print(cr.getRandom());
   }
  %>
  <jsp:getProperty name="ci" property="info"/><br>  
  <form method="POST" action="action.jsp">
   <p>猜数字:<input type="text" name="guessNumber" size="20"></p>
   <input type="submit" value="登录" name="B1">
  </form>
</body>
</html>

用于判断并且实现分发跳转的action.jsp文件为:

<%
String s1=request.getParameter("guessNumber");
int num1=Integer.parseInt(s1);

mypro.CRandom cr=(mypro.CRandom)session.getAttribute("cr");
int num2=cr.getRandom();

mypro.CInfo ci=(mypro.CInfo)session.getAttribute("ci");

if(num1==num2)
{
  out.print("OK!<p><a href='exec.jsp'>Please click this to continue...</a>");
  session.invalidate();
}
else if(num1<num2)
{
  ci.setInfo("lower");
  RequestDispatcher dispatcher = request.getRequestDispatcher("exec.jsp");
  dispatcher.forward(request, response);
}
else
{
  ci.setInfo("higher");
  RequestDispatcher dispatcher = request.getRequestDispatcher("exec.jsp");
  dispatcher.forward(request, response);
}
%>

[此贴子已经被作者于2010-12-12 08:16:59编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
admin
  2楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
更为简单的exec.jsp网页  发帖心情 Post By:2007/5/17 22:08:03 [只看该作者]

关于如何将设置Bean属性的操作只进行一次,除了前面的方法外,更为简单的做法是将setProperty标签嵌套到对应的useBean标签即可,如:

<%@page c%>
<html>
<head>
</head>
<body>
  <jsp:useBean id="cr" scope="session" class="mypro.CRandom">
   <jsp:setProperty name="cr" property="initialNumber" value="100"/>
  </jsp:useBean>
  <jsp:useBean id="ci" scope="session" class="mypro.CInfo">
   <jsp:setProperty name="ci" property="info" value="Hello!"/>
  </jsp:useBean>
  <jsp:getProperty name="ci" property="info"/><br>  
  <form method="POST" action="action.jsp">
   <p>猜数字:<input type="text" name="guessNumber" size="20"></p>
   <input type="submit" value="登录" name="B1">
  </form>
</body>
</html>
  

[此贴子已经被作者于2010-12-12 08:17:14编辑过]

 回到顶部