今天呢,我们来编写一个猜数字的小程序;再编写之前列几个代码意义:
1、get提交与post提交;get提交提交的信息会显示在地址栏中,而post提交提交的信息不会显示在地址栏中。
2、随机产生一个1到100的整数:target=(int)(Math.random() * 100) + 1;其中Math.random()表示随机产生0到1之间的数不包括0和1,Math.random() * 100表示随机产生一个0到99之间的数。
3、request.getParameter;这个代码是提取form中的参数。
4、document.getElementById;这个代码是获取某个id
5、hidden;这个属性是对元素进行隐藏。
一、我们先创建一个onclass04项目
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190928114141791.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTAwMjYwNA==,size_16,color_FFFFFF,t_70)
二、随后就是在web包下创建numguess02.jsp和index.html
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190928114309234.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTAwMjYwNA==,size_16,color_FFFFFF,t_70)
三、接下来我们就要开始编写代码了
<%@ page import="java.io.IOException" %><%--
Created by IntelliJ IDEA.
User: 周杰
Date: 2019/9/23
Time: 20:16
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%
//电脑猜测的数
int target = 0;
if (session.getAttribute("target") == null) {
//随机产生一个1到100的整数
target = (int)(Math.random() * 100) + 1;
//将猜测目标存放在session中
session.setAttribute("target",target);
}else {
//从猜测目标中获取电脑的猜测数
target = (int)session.getAttribute("target");
}
//用户猜测次数
int count = 0;
if (session.getAttribute("count") == null) {
//在session里创建一个count属性
session.setAttribute("count",count);
}else {
//将count在session中取出来
count = (int)session.getAttribute("count");
}
String message="";
try {
//获取用户猜测的数字
String strNum = request.getParameter("num"); //该代码是提取form中的参数:num(用户猜测的数字)
int num = Integer.parseInt(strNum);//该代码是将strNum属性转换成int类型
//对用户猜测的数字与电脑猜测的数字进行比较
if (num > target){
//每进行一次比较,猜测次数就加一次
count++;
//再将猜测次数重新写入session中
session.setAttribute("count",count);
if (count > 1) {
message = "Good guess,but nope. Try <b>lower</b>. You have made" + count + "guesses.";
}else {
message = "Good guess,but nope. Try <b>lower</b>. You have made" + count + "guesse.";
}
}else if (num < target){
//每进行一次比较,猜测次数就加一次
count++;
//再将猜测次数重新写入session中
session.setAttribute("count",count);
if (count > 1) {
message = "Good guess,but nope. Try <b>higher</b>. You have made" + count + "guesses.";
}else {
message = "Good guess,but nope. Try <b>higher</b>. You have made" + count + "guesse.";
}
}else {
message = "恭喜你猜对了"; //用户猜对后最终显示的信息
//猜对后要删除session中的猜测数和猜测次数
session.removeAttribute("target");
session.removeAttribute("count");
out.print("Congratulations! You got it.And after just 8 tries" + count + "tries.<br/>" +
"Care to <a href='numguess02.jsp'>try again</a>?");
}
} catch (NumberFormatException e) {
System.out.println("erro:" + e.getMessage());
}
%>
<%--将message显示在id为prompt的<p>标签里,涉及到js与jsp之间的通信--%>
<script>
var message = "<%= message%>";
var prompt = document.getElementById("prompt");//document.getElementById这个方法是获取某个id
prompt.innerHTML=message;
if (message == "恭喜你猜对了") {
document.getElementById("tt").hidden=true;//hidden属性是对元素进行隐藏,隐藏的元素不会显示
document.getElementById("frmNumGuess").hidden=true;
}
</script>
五、最终的成果 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190928114837722.gif)