使用JavaBean实现猜数字小游戏

使用JavaBean实现猜数字小游戏

使用JavaBean实现猜数字小游戏_第1张图片
样例跳转:样例七<–点这跳转

index.jsp

<%@ page import="java.util.Random" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/4/24
  Time: 16:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
    <head>
        <title>猜数字游戏</title>
        <jsp:useBean id="guess" class="lib.GuessNumber" scope="application"/>
    </head>
    <body>
        <h3>随机系统随机生成了一个1100之间的整数,请猜是什么数?</h3>
        <%
            Random randomNumbers=new Random();
            int answer=1+randomNumbers.nextInt(100);
            String str=response.encodeRedirectURL("guess.jsp");
        %>
        <jsp:setProperty name="guess" property="answer" value="<%=answer%>"/>
        <hr>
        <form name="from1" action="<%=str%>" method="get">
            输入你的猜的数:
            <input type="text" name="guessNumber">
            <input type="submit" value="提交">
        </form>
    </body>
</html>

guess.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/4/24
  Time: 16:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
    <head>
        <title>猜数字游戏</title>
    </head>
    <body>
        <jsp:useBean id="guess" class="lib.GuessNumber" scope="session"/>
        <%
            String  strGuess=response.encodeRedirectURL("guess.jsp"),
            strGetNumber=response.encodeRedirectURL("index.jsp");
        %>
        <hr>
        <jsp:setProperty name="guess" property="guessNumber" param="guessNumber"/>
        这是第<jsp:getProperty name="guess" property="guessCount"/>次猜。
        <jsp:getProperty name="guess" property="result"/>
        你猜的数是<jsp:getProperty name="guess" property="guessNumber"/><!--答案是<jsp:getProperty name="guess" property="answer"/>-->
        <%
            if(!guess.isRight()){
        %>
        <form action="<%=strGuess%>" method="get">
            请再猜一次:
            <input type="text" name="guessNumber">
            <input type="submit" value="提交">
        </form>
        <%
            }
        %>
        <hr>
        <a href="<%=strGetNumber%>">重新开始猜数</a>
    </body>
</html>

GuessNumber.java

package lib;
public class GuessNumber{
    int answer=43;
    int guessNumber=0;
    int guessCount=0;
    String result=null;
    boolean right=false;
    public void setAnswer(int answer){
        this.answer=answer;
        guessCount=0;
    }
    public int getAnswer(){
        return answer;
    }
    public int getGuessNumber(){
        return guessNumber;
    }
    public void setGuessNumber(int guessNumber){
        this.guessNumber=guessNumber;
        guessCount++;
        if(guessNumber==answer){
            result="恭喜你猜对了!";
            right=true;
        }
        else if(guessNumber>100||guessNumber<=0){
            result="请输入1-100的整数";
            right=false;
        }
        else if(guessNumber>answer){
            result="不好意思,你猜大了!";
            right=false;
        }
        else if(guessNumber<answer){
            result="不好意思,你猜小了!";
            right=false;
        }
    }

    public void setGuessNumber() {
        this.guessNumber = guessNumber;
    }

    public void setGuessCount(int guessCount) {
        this.guessCount = guessCount;
    }

    public int getGuessCount() {
        return guessCount;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public boolean isRight() {
        return right;
    }

    public void setRight(boolean right) {
        this.right = right;
    }
}

使用JavaBean实现猜数字小游戏_第2张图片
使用JavaBean实现猜数字小游戏_第3张图片
使用JavaBean实现猜数字小游戏_第4张图片

你可能感兴趣的:(JavaWeb)