JSP---Guessing number game, deep feeling

Source: Internet
Author: User

Guess the number game note two points, 1. The random number and the guessing number cannot be placed on the same page, otherwise the random number will not stop appearing, guess the number cannot be equal.

2. Conversion of data types to each other. Wrapper class integer and int are used, the former is the class, the latter is the basic data type

cai.jsp

  

?
1 2 3 4 5 6 7 8 9 <body> 去猜数字----<a href="b.jsp">guess</a>  <%   int a=(int)(Math.random()*100);   session.setAttribute("number",a);     %>  <%="随机数为那:"+session.getAttribute("number") %> </body>

b.jsp

?
1 2 3 4 5 6 <body> <form action="da.jsp"> <input type="text" name="guess"/><br/> <input type="submit" value="提交"/></form>

da.jsp

  

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <body>  <%       Integer str1=(Integer)session.getAttribute("number");                 String str2=request.getParameter("guess");       int num2=Integer.parseInt(str2);       int num1=Integer.valueOf(str1);       if(num1==num2){           out.print("您好,猜对了,再玩一次,<a href=‘cai.jsp‘>guess</a>");              }else if(num1>num2){           out.print("您好,猜小了,再猜一次,<a href=‘b.jsp‘>guess</a>");             }else if(num1<num2){           out.print("您好,猜大了,再猜一次,<a href=‘b.jsp‘>guess</a>");       }            %>

  

      

    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.