/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.cstp.java.RandomNum;
import com.yourcompany.struts.form.GuessNumForm;
/**
* MyEclipse Struts
* Creation date: 07-30-2008
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class GuessNumAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
GuessNumForm gnf = (GuessNumForm)form;
RandomNum random = new RandomNum();
ActionForward result = null;
String flag = request.getParameter("flag");
//
if(flag == null)
{
flag = "";
}
if(flag.length()>0)
{
String randomString = (String)request.getSession().getAttribute("randomNum");
String inputString = gnf.getNum1()+gnf.getNum2()+gnf.getNum3()+gnf.getNum4();
/*if(!random.isStringInRange(inputString,"0123456789"))
{
String isInRange = "<script language=\"javascript\"> alert('输入错误') </script>";
request.setAttribute("isInRange",isInRange);
}
*/
gnf.setUserInputNum(inputString);
gnf.setNum1("");
gnf.setNum2("");
gnf.setNum3("");
gnf.setNum4("");
String isCount = random.isCount(randomString, inputString);//上面显示的值
String isCount1 = random.isCount(randomString, inputString);//累加的值
isCount1 = request.getParameter("isCount1") + "," + isCount1 + ":" + gnf.getUserInputNum();
String[] successArray = isCount1.split(",");
request.setAttribute("userInputNum",gnf.getUserInputNum());
request.setAttribute("isCount1",isCount1);
request.setAttribute("successArray",successArray);
if(isCount.equals("4A0B"))
{
String success = "<font color=\"red\" size=\"+1\">恭喜你,猜对了!!!</font><br/>" + "<a href=\"guessNum.do\">重新开始<a>" +
"<script language=\"javascript\">guessNumForm.B1.disabled = true;</script>";
request.setAttribute("success",success);
result = mapping.findForward("GoToGuessNum");
}
request.setAttribute("isCount",isCount);
}
else
{
String randomNum = random.getRandomInt(4);
System.out.println(randomNum);
request.getSession(true).setAttribute("randomNum",randomNum);
}
return result = mapping.findForward("GoToGuessNum");
}
}