题目要求:
1、用户可以自己输入题目数量和每行题目个数
2、可实现在线答题和判题功能。
3、生成的题目中,减法的结果不能为负数,乘法不允许结果大于100,除法必须能够整除。
程序代码:
jiemian.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>四则运算出题器</title> </head> <body> <h2 align="center" >四则运算出题器.</h2> <form name="m1" action="show.jsp" method="post" onsubmit="return check2();" > <table align="center"> <tr> <td>题目数量:</td> <td><input type="text" name="n1" vlaue="" onchange="check()"/></td> </tr> <tr> <td>每行题目数量:</td> <td><input type="text" name="n2" vlaue="" onchange="check1()"/></td> </tr> </table> <p align="center" ><input type="submit" value="开始答题"></p> <script type="text/javascript"> function check(){ var n3=m1.n1.value; if(n3.length==0) { alert("题数不能为空"); return false; } } function check1(){ var n4=m1.n2.value; if(n4.length==0) { alert("每行题数不能为空"); return false; } } function check2(){ var n3=m1.n1.value; var n4=m1.n2.value; if(n3.length==0) { alert("题数不能为空"); return false; } if(n4.length==0) { alert("每行题数不能为空"); return false; } } </script> </form> </body> </html>
show.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>出题界面</title> </head> <body> <h2 align="center" >题目如下</h2> <% int h=Integer.parseInt(request.getParameter("n2")); int x=Integer.parseInt(request.getParameter("n1")); String timu[]=new String[x]; String daan[]=new String[x]; int i; for(i=0;i<x;) { int num1=(int)(1+(Math.random()*100)); int num2=(int)(1+(Math.random()*100)); int k=(int)(1+(Math.random()*4)); if(k==1) { timu[i]=num1+"+"+num2+"="; daan[i]=num1+num2+""; i++; } else if(k==2&&num1>=num2) { timu[i]=num1+"-"+num2+"="; daan[i]=num1-num2+""; i++; } else if(k==3&&num1*num2<100) { timu[i]=num1+"*"+num2+"="; daan[i]=num1*num2+""; i++; } else if(k==4&&num2!=0&&num1%num2==0) { timu[i]=num1+"/"+num2+"="; daan[i]=num1/num2+""; i++; } else continue; } %> <form action="panduan.jsp" method="post" > <table align="center"> <% int k; for(int j=0;j<x;) { k=0; %> <tr> <% while(k<h) { %> <td><%=timu[j] %></td> <td><input type="hidden" name="timu" value=<%=timu[j] %>></td> <td><input type="text" name="jieguo" /></td> <td><input type="hidden" name="daan" value=<%=daan[j] %>></td> <% j++; k++; if(j>=x) break; } %> </tr> <% } %> </table> <p align="center" ><input type="submit" value="提交"></p> </form> </body> </html>
panduan.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>判断对错</title> </head> <body> <h2 align="center" >结果如下</h2> <% int a=0,b=0; String jieguo[]=request.getParameterValues("jieguo"); String daan[]=request.getParameterValues("daan"); String timu[]=request.getParameterValues("timu"); for(int i=0;i<timu.length;i++) { if(jieguo[i].equals(daan[i])) { %> <p align="center" style="color: green;"><%=timu[i]%> 回答正确</p> <% a++; } else { %> <p align="center" style="color: red;"><%=timu[i] %><%=jieguo[i]%>? 回答有误,正确答案:<%=daan[i] %></p> <% b++; } } %> <form action="jiemian.jsp" method="post" > <h2 align="center">总共<%=timu.length %>道题,回答正确<%=a %>道,回答有误<%=b %>道</h2> <p align="center" ><input type="submit" value="继续答题"></p> </form> </body> </html>