四则运算
程序设计思想:先定义一个.java类,通过随机数函数,来产生试题,产生试题的函数为string类型,最后返回表达式。然后创建HTML和JSP文件,
用于编写登录界面可以选择试题数量及考试时间。然后在编写判断正确率的一个文件,用于判断所答试题的正确率。
代码:
jisuan.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title style="color:red">四则运算</title> <meta charset="UTF-8"> </head> <body style="font-size:20px" background="D:\tupian\yuyu.jpg"> <center> <h1 style="color:red">四则运算</h1> <form action="jisuancheck.jsp" method="get"> <table style="font-size:30px"> <tr> <td> 请选择测试时间:</td> <td><input type="radio" name="time" value="1">1分钟 <input type="radio" name="time" value="5" >5分钟 <input type="radio" name="time" value="10">10分钟 </td> </tr> <tr> <td>请选择测试的题目数量:</td> <td> <select name="number"> <option value="30">30</option> <option value="40">40</option> <option value="50">50</option> <option value="60">60</option> </select> </td> </tr> <tr> <td> <center> <input type = "submit" value = ""style="margin-top:150px;width:250px;height:250px; background:url(D:/tupian/op.png);background-size:250px 250px; border-style:none;"/> </center> </td> </tr> </table> </form> </center> </body> </html>
jisuan.java
package yu; import java.util.Scanner; public class jisuan{ int e1; int e2; int e3; int e4; int e5; int e0; int k; public String showQuestion() { int a; int b; int c; int d; Scanner sc = new Scanner(System.in); a=(int) (Math.random()*100); b=(int) (Math.random()*100); c=(int) (Math.random()*100); d=(int) (Math.random()*6); if(d==1) { while(a+b+c>100) { a=(int) (Math.random()*100); b=(int) (Math.random()*100); c=(int) (Math.random()*100); } e1=a+b+c; k=e1; return (a+"+"+b+"+"+c); } if(d==2) { while(a-b-c<0) { a=(int) (Math.random()*100); b=(int) (Math.random()*100); c=(int) (Math.random()*100); } e2=a-b-c; k=e2; return (a+"-"+b+"-"+c); } if(d==3) { while(a+b-c<0||a+b>100) { a=(int) (Math.random()*100); b=(int) (Math.random()*100); c=(int) (Math.random()*100); } e3=a+b-c; k=e3; return (a+"+"+b+"-"+c); } if(d==4) { while(a-b<0||a-b+c>100) { a=(int) (Math.random()*100); b=(int) (Math.random()*100); c=(int) (Math.random()*100); } e4=a-b+c; k=e4; return (a+"-"+b+"+"+c); } if(d==5) { while(a*b>100) { a=(int) (Math.random()*100); b=(int) (Math.random()*100); c=(int) (Math.random()*100); } e5=a*b; k=e5; return a+"*"+b; } if(d==0) { while(a%b!=0) { a=(int) (Math.random()*100); b=(int) (Math.random()*100+1); c=(int) (Math.random()*100); } e0=a/b; k=e0; return a+"÷"+b; } return null; } public int kk() { return k; } }
jisuancheck.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@page import = "yu.jisuan" %> <html> <head> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>四则运算出题界面</title> <% int num = 0; request.setCharacterEncoding("utf-8"); if(request.getParameter("number")!=null){ num=(Integer.parseInt(request.getParameter("number"))); }%> </head> <body background="D:\tupian\yu.jpg"> <div id="timer" style="color:red"></div> <center> <form id="myForm" action="jieguo.jsp" method = "get"> <table border="1" > <tr> <td>题号</td> <td>题目</td> <td>答案</td> </tr> <jsp:useBean id="js" class="yu.jisuan"/> <% jisuan kt[] = new jisuan[60]; String sk2=""; int i; for(i=0;i<num;i++){ kt[i]=new jisuan(); %> <tr> <td><%=i+1 %></td> <td><%=kt[i].showQuestion() %></td> <td width="50px">=<input name="result" id="a<%=i + 1%>" width="8px" style="width: 30px"></td> <td><div id="r<%=i + 1%>"></div></td> </tr> <% sk2 +=kt[i].kk() +"/"; session.setAttribute("right", sk2); } %> <tr> <td colspan="3" align="right"> <input type = "submit" value = "完成"/> </td> </tr> </table> <% int tm = 0; request.setCharacterEncoding("utf-8"); if(request.getParameter("time")!=null){ tm=(Integer.parseInt(request.getParameter("time"))); }%> <script type="text/javascript"> var maxtime = "<%=tm*60%>"; // 半个小时,按秒计算,自己调整! function CountDown(){ if(maxtime>=0){ var minutes = Math.floor(maxtime/60); var seconds = Math.floor(maxtime%60); var msg = "距离结束还有"+minutes+"分"+seconds+"秒"; document.all["timer"].innerHTML=msg; if(maxtime == 1*30) alert('注意,还有半分钟!'); --maxtime; } else{ clearInterval(timer); alert("时间到,结束!"); } } timer = setInterval("CountDown()",1000); setTimeout('window.location="jieguo.jsp"',maxtime*1000); setTimeout( "submitFun()", maxtime*1000 ); function submitFun() { document.getElementById("myForm").submit(); } </script> </form> </center> </body> </html>
jieguo.jsp
<%@page import = "yu.jisuan" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body background="D:\tupian\jiayou.jpg"> <center> <% String result[] = request.getParameterValues("result"); String str = (String)session.getAttribute("right"); String[] right = str.split("/"); int countright = 0; double cod; for(int i=0;i<right.length;i++) { if(right[i].equals(result[i])) { out.println("第"+(i+1)+"题回答正确!"); countright++; } else { out.println("第"+(i+1)+"题回答错误!正确答案为:"+right[i]); } %> <br> <% } %> <% cod=countright; cod=cod/30*100; %> <%="共答对了"+countright+"道题"%> <%="正确率为"%> <%=cod+"%" %> <br> <td><a href = "jisuan.html">返回选择界面</a> </center> </body> </html>
截图: