1

我有一个 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

4

3 回答 3

1

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

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


更好的是,使用 JavaScript!

于 2013-04-24T20:28:35.873 回答
1

在这里找到完整的程序:http: //sickprogrammersarea.blogspot.in/2014/01/creating-number-guesser-in-jsp_8296.html

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

数字.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>

尝试.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>
于 2014-03-12T18:56:00.080 回答
0

You could pass the user number back as a query parameter.

I'm guessing you are not too familiar with the MVC pattern or Spring, but there are plenty of resources available to get you quickly up and running. I'll try to make a strip down demo later, if I remember.

于 2013-04-24T20:31:43.847 回答