久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

JSP實現(xiàn)百萬富翁猜數(shù)字游戲

瀏覽:50日期:2022-06-07 14:37:42

本文實例為大家分享了JSP實現(xiàn)百萬富翁猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下

設計一個web app,每次產(chǎn)生一個30以內(nèi)的數(shù)字,給5次機會讓客戶猜測這個數(shù)字:

1)如果客戶猜的數(shù)字比產(chǎn)生的數(shù)字值大,則提示“大了”。
2)如果客戶猜的數(shù)字比產(chǎn)生的數(shù)字值小,則提示“小點”

猜對了就過關,猜錯Game Over,給玩家重玩的機會。

JSP代碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% 
 String result=(String)request.getAttribute("result");
 if(result!=null){
  out.write("<font color="red">"+result+""</font>");
 }
%>

<% 
 Integer times=(Integer)request.getAttribute("times");
 if(times!=null){
  out.write("你還有"+(5-times)+"次機會!");
 }
%>
<br/>
<form action="/zxz/zxz" method="POST">
 請輸入你的數(shù)(20以下):<input type="text" name="Lucy" /><br/>
 <%
 if(times!=null){
 %>
  <input type="hidden" name="times" value="<%=times %>"/>
 <% 
 }
 %>
 <input type="submit" value="競猜" />
</form>
</body>
</html>

Servlet代碼:

package hah;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class zxz
 */
@WebServlet("/zxz")
public class zxz extends HttpServlet {
 private static final long serialVersionUID = 1L;

 int answer;
 public void newGame() {
 Random random=new Random();
 answer=random.nextInt(20);
 }
 public zxz() {
 newGame();
 }
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 response.setContentType("text/html;charset=utf-8");
 String lucyStr=request.getParameter("Lucy");
 Integer lucyNb=null;
 System.out.println("答案:"+answer);
 if(!lucyStr.equals("")) {
  lucyNb=Integer.parseInt(lucyStr);
 }
 Integer times=1;

 String timeStr=request.getParameter("times");
 if(timeStr!=null&&!timeStr.equals("")) {
  times=Integer.parseInt(timeStr)+1;
 }
 if(times<5) {
  String result="";
  if(lucyNb>answer) {
  result="大了";  
  }else if(lucyNb<answer) {
  result="小了";
  }else if(lucyNb==answer) {
  result="中了";
  times=null;
  }
  request.setAttribute("times", times);
  request.setAttribute("result", result);
 }else {
  newGame();
  response.getWriter().write("游戲結束<a href=""+request.getContextPath()+"/One.jsp">再來一把</a>");
  return;
 }
 request.getRequestDispatcher("/One.jsp").forward(request, response);
 }


 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doGet(request, response);
 }

}

總結:

a. 使用標簽hidden可以隱式傳遞數(shù)據(jù)而不被用戶發(fā)現(xiàn) 可以用來記錄次數(shù) 如:

<input type="hidden" name="times" value="<%=times %>"/>

b. Servlet是用來跳轉和執(zhí)行邏輯代碼的,JSP是用來展示數(shù)據(jù)的
c. request.getParameter(“Lucy”);如果參數(shù)不存在則返回null的字符串值
d 跳轉有兩種方式 一個是頁面跳轉 地址要寫項目名+jsp或者servlet

另一個是轉發(fā)共享了request的域對象,地址可以直接寫jsp或者servlet 不要項目名 而且項目名和jsp或者servlet前都要加“/” 不然就是相對位置了

如:

<form action="/zxz/zxz" method="POST">
//轉發(fā)
request.getRequestDispatcher("/One.jsp"). 
forward(request, response);

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持。

標簽: JSP
主站蜘蛛池模板: 枣庄市| 建瓯市| 栾川县| 舒兰市| 蒙阴县| 隆安县| 汾阳市| 桃园市| 漾濞| 蚌埠市| 三门峡市| 江山市| 鄂温| 新乡市| 双牌县| 荣成市| 南开区| 和平区| 资阳市| 晴隆县| 通渭县| 宁南县| 天峨县| 忻州市| 柯坪县| 沅江市| 怀化市| 晴隆县| 商水县| 万载县| 余庆县| 敦化市| 南丰县| 澄江县| 班玛县| 策勒县| 中方县| 兴隆县| 依兰县| 三门县| 响水县|