您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > jsp-实现猜数字游戏
(1)ex3_index.jsp%@pagecontentType=text/html;charset=gb2312%htmlheadtitleMyJSP'ex3_index.jsp'startingpage/title/headbodyp系统随机分配了一个1到100之间的整数,请猜!/pP输入你所猜的数:FORMaction=result.jspmethod=postname=forminputtype=textname=guessinputtype=submitname=submitvalue=送出/FORM%intnumber=(int)(Math.random()*100+1);session.setAttribute(count,newInteger(0));//猜数次数session.setAttribute(save,newInteger(number));//存储正确的数session.setAttribute(minNumber,newInteger(1));//最小值存储session.setAttribute(maxNumber,newInteger(100));//最大值存储%/body/html(2)result.jsp%@pagecontentType=text/html;charset=gb2312%htmlheadtitleMyJSP'result.jsp'startingpage/title/headbodyPThisistheresultpage(hidden).br%Stringstr=request.getParameter(guess);//从文本框中获取字符串intguessNumber=Integer.parseInt(str);//将字符串转换成整数intmax=((Integer)session.getAttribute(maxNumber)).intValue();intmin=((Integer)session.getAttribute(minNumber)).intValue();Integerinteger=(Integer)session.getAttribute(save);intrealnumber=integer.intValue();if(guessNumber==realnumber){intn=((Integer)session.getAttribute(count)).intValue();n=n+1;session.setAttribute(count,newInteger(n));response.sendRedirect(success.jsp);}elseif(guessNumberrealnumber){intn=((Integer)session.getAttribute(count)).intValue();n=n+1;max=guessNumber;session.setAttribute(count,newInteger(n));session.setAttribute(maxNumber,newInteger(max));response.sendRedirect(larger.jsp);}elseif(guessNumberrealnumber){intn=((Integer)session.getAttribute(count)).intValue();n=n+1;min=guessNumber;session.setAttribute(count,newInteger(n));session.setAttribute(minNumber,newInteger(min));response.sendRedirect(smaller.jsp);}%/body/html(3)larger.jsp%@pagecontentType=text/html;charset=gb2312%htmlheadtitleMyJSP'larger.jsp'startingpage/title/headbody%intmax=((Integer)session.getAttribute(maxNumber)).intValue();intmin=((Integer)session.getAttribute(minNumber)).intValue();%Thisislargerpage.brP所猜的数比实际的数大,请再猜:/P从%=min%到%=max%FORMaction=result.jspmethod=getname=formINPUTtype=textname=guessINPUTtype=submitvalue=送出name=submit/FORM/body/html(4)smaller.jsp%@pagecontentType=text/html;charset=gb2312%htmlheadtitleMyJSP'smaller.jsp'startingpage/title/headbody%intmax=((Integer)session.getAttribute(maxNumber)).intValue();intmin=((Integer)session.getAttribute(minNumber)).intValue();%Thisissmallerpage.brP所猜的数比实际的数小,请再猜:/P从%=min%到%=max%FORMaction=result.jspmethod=postname=formINPUTtype=textname=guessINPUTtype=submitvalue=送出name=submit/FORM/body/html(5)success.jsp%@pagecontentType=text/html;charset=gb2312%htmlheadtitleMyJSP'success.jsp'startingpage/title/headbodyThisissuccesspage.br%intcount=((Integer)session.getAttribute(count)).intValue();intnum=((Integer)session.getAttribute(save)).intValue();longstartTime=session.getCreationTime();longendTime=session.getLastAccessedTime();%P恭喜您,猜对了!/PP您共猜了%=count%次/PP用时%=(endTime-startTime)/1000%秒/PP这个数字就是%=num%/PP您必须关掉浏览器才能获得新的数/P/body/html实验结果:
本文标题:jsp-实现猜数字游戏
链接地址:https://www.777doc.com/doc-1511469 .html