数字猜测器游戏每次猜测产生不同的数字

时间:2013-04-24 20:23:33

标签: java jsp random

我有一个Java服务器页面,允许用户从1-1000中选择一些他们的选择。然后该页面使用输入的数字,并查明该数字是否与生成的数字相匹配。 - 如果^不清楚,下面的图片。目前,每当用户猜出一个数字时,程序就会生成一个不同的数字 - 无论它是否正确。如何使程序只在用户刷新页面或正确猜测时生成一个数字?

JSP代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import = "Chapter12.RandomGuess" %>
<jsp:useBean id = "randomGuessId" class = "Chapter12.RandomGuess" scope = "page" >
</jsp:useBean>
<jsp:setProperty name = "randomGuessId" property = "*" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guess Random Number</title>
</head>
<body>
<h3>Guess a number from 1 to 1000</h3>
<form method = "post">
    Enter guess: <input name = "guess" /><br /><br />
    <input type = "submit" name = "Submit" value = "Take Guess" />
    <input type = "reset" value = "Reset" /><br /><br />
    Your guess of <jsp:getProperty name = "randomGuessId" property="guess" />
    is <%= RandomGuess.guess(randomGuessId.getAnswer()) %>
    </form>
</body>
</html>

Java代码:

import java.util.Random;

public class RandomGuess {
private int guess;
Random r = new Random();
private int low = 1;
private int high = 1000;
int R = r.nextInt(high-low) + low;

public int getGuess() {
    return guess;
}

public void setGuess(int newValue) {
    guess = newValue;
}

public String getAnswer() {
    String tooLow = "too low.";
    String tooHigh = "too high.";
    String correct = "correct!";
    if(guess == R) 
        return correct;
    else if(guess < R) 
        return tooLow;
    else
        return tooHigh;
}   
public static String guess(String s) {
    return s;
}
}

图片:http://i.imgur.com/dMSZ7SD.png

3 个答案:

答案 0 :(得分:1)

每次刷新页面时,都会创建一个新的bean实例 - 使用新编号。

要保留通话中的号码,请在班级中使用静态字段。


更好的是,使用JavaScript!

答案 1 :(得分:1)

在此处查找完整的计划:http://sickprogrammersarea.blogspot.in/2014/01/creating-number-guesser-in-jsp_8296.html

朋友您好这是一个在JSP中创建数字猜测器的简单程序....看看......

<强> Number.jsp:

<html>
<head><title>Number Guesser</title></head>
<body>
<%              int num=(int)(Math.random()*100);        %>
<h2>Welcome To Number Guesser</h2>
<br><h3>Want To Check Your Guessing Power.....GIVE IT A TRY?????</h3>
<form name="guess" action="try.jsp?c=0" method="post">
<input type="text" name="val" value="<%=num %>" hidden>
<input type="submit" value="GO">
</form>
</body>
</html>

try.jsp:

<html>
<head><title>Number Guesser</title></head>
<body>
<%    String str=request.getParameter("val");
      boolean flag=true;      
      int num=Integer.parseInt(str);
      int c=Integer.parseInt(request.getParameter("c"));
      if(c!=0)
      {
                  int guess=Integer.parseInt(request.getParameter("guess"));
                  if(num==guess){
                              flag=false;
      %>
                  <h3>Congratulation...You Successes After <%=c%> attempts</h3>
                  <b>Want to Improve...<a href="Number.jsp">try again</a></b>
      <%       }else if(num>guess) { %>
                  <h3>You Guessed Lower...Try Bigger Number</h3>
      <%       }else{ %>       
                  <h3>You Guessed Higher...Try Smaller Number</h3>
      <%       }
      }
      if (flag) { c++; %>     
      <h3>I Guessed a Number between 1 to 100. Try To Guess..</h3>
      <form name="guess" action="try.jsp?c=<%= c%>" method="post">
      <input type="text" value="<%=num %>" name="val" hidden>
      Make Your Guess : <input type="text" name="guess" size=10 maxlength=3>
      <input type="submit" value="GO">
      </form>
      <% } %>

</body> 
</html>

答案 2 :(得分:0)

您可以将用户编号作为查询参数传回。

我猜你不太熟悉MVC模式或Spring,但有足够的资源可以让你快速启动和运行。如果我记得的话,我会稍后尝试制作一个带状演示。