主頁(yè) > 知識(shí)庫(kù) > JSP實(shí)現(xiàn)百萬(wàn)富翁猜數(shù)字游戲

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

熱門標(biāo)簽:南通通訊外呼系統(tǒng)產(chǎn)品介紹 電銷機(jī)器人免培訓(xùn) 給地圖標(biāo)注得傭金 電話機(jī)器人需要使用網(wǎng)絡(luò)嗎 海外圖書館地圖標(biāo)注點(diǎn) 潤(rùn)滑油銷售電銷機(jī)器人 如何看懂地圖標(biāo)注點(diǎn) 自繪地圖標(biāo)注數(shù)據(jù) 外呼系統(tǒng)使用方法

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

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

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

猜對(duì)了就過關(guān),猜錯(cuò)Game Over,給玩家重玩的機(jī)會(huì)。

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)+"次機(jī)會(huì)!");
 }
%>
br/>
form action="/zxz/zxz" method="POST">
 請(qǐng)輸入你的數(shù)(20以下):input type="text" name="Lucy" />br/>
 %
 if(times!=null){
 %>
  input type="hidden" name="times" value="%=times %>"/>
 % 
 }
 %>
 input type="submit" value="競(jìng)猜" />
/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(times5) {
  String result="";
  if(lucyNb>answer) {
  result="大了";  
  }else if(lucyNbanswer) {
  result="小了";
  }else if(lucyNb==answer) {
  result="中了";
  times=null;
  }
  request.setAttribute("times", times);
  request.setAttribute("result", result);
 }else {
  newGame();
  response.getWriter().write("游戲結(jié)束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);
 }

}

總結(jié):

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

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

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

另一個(gè)是轉(zhuǎn)發(fā)共享了request的域?qū)ο?/strong>,地址可以直接寫jsp或者servlet 不要項(xiàng)目名 而且項(xiàng)目名和jsp或者servlet前都要加“/” 不然就是相對(duì)位置了

如:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 基于JavaScript實(shí)現(xiàn)猜數(shù)字游戲代碼實(shí)例
  • JS猜數(shù)字游戲?qū)嵗v解
  • JavaScript實(shí)現(xiàn)猜數(shù)字游戲
  • JS實(shí)現(xiàn)網(wǎng)頁(yè)端猜數(shù)字小游戲
  • jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲
  • AngularJS實(shí)現(xiàn)的生成隨機(jī)數(shù)與猜數(shù)字大小功能示例
  • angularjs實(shí)現(xiàn)猜數(shù)字大小功能
  • js實(shí)現(xiàn)一個(gè)猜數(shù)字游戲
  • js猜數(shù)字小游戲的簡(jiǎn)單實(shí)現(xiàn)代碼
  • 純JavaScript實(shí)現(xiàn)猜數(shù)字游戲

標(biāo)簽:廣州 內(nèi)江 樂山 銅川 貸款邀約 大連 南京 黃石

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP實(shí)現(xiàn)百萬(wàn)富翁猜數(shù)字游戲》,本文關(guān)鍵詞  JSP,實(shí)現(xiàn),百萬(wàn)富翁,猜,數(shù)字,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《JSP實(shí)現(xiàn)百萬(wàn)富翁猜數(shù)字游戲》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于JSP實(shí)現(xiàn)百萬(wàn)富翁猜數(shù)字游戲的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章