web版四则运算
Posted heartbeat
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web版四则运算相关的知识,希望对你有一定的参考价值。
题目:实现网页上的四则运算练习
设计思路:(整数为例)首先首页为选择做题的类型,用单选框进行选择,之后跳转到相应的页面进行题目规范的输入,例如:最大最小值,输入后进行提交,确认输入的信息无误,如果有误跳回页面,无误后转入出题页面,出一道题写一道题的答案,之后用session进行答案等的保存,将值传入到判断正误的页面,对输出正确,错输出错误和正确结果,题目全部做完进行结果的查看,点击返回主页,则返回到主页。主要是各种值得保存和调用,还有输入规范的判断。(分数和带括号的式子同理)。
源代码:
choose.jsp:
<%@ page contentType = "text/html; charset=utf-8" import = "java.sql.*" errorPage = "error.jsp" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>选择</title> <style> body { background: #222; margin: auto; width: 960px; } .arc-text { font: 16px sans-serif; } .arc-center { fill: none; } #credit { position: absolute; font: 10px sans-serif; right: 10px; bottom: 10px; color: #ddd; } #credit a { color: inherit; } </style> <div id="credit">Inspired by <a href="http://blog.pixelbreaker.com/polarclock/">pixelbreaker</a>.</div> <script src="//d3js.org/d3.v3.min.js"></script> <script> var width = 200, height = 200, radius = Math.min(width, height) / 1.6, spacing = .09; var formatSecond = d3.time.format("%-S seconds"), formatMinute = d3.time.format("%-M minutes"), formatHour = d3.time.format("%-H hours"), formatDay = d3.time.format("%A"), formatDate = function(d) { d = d.getDate(); switch (10 <= d && d <= 19 ? 10 : d % 10) { case 1: d += "st"; break; case 2: d += "nd"; break; case 3: d += "rd"; break; default: d += "th"; break; } return d; }, formatMonth = d3.time.format("%B"); var color = d3.scale.linear() .range(["hsl(-180,60%,50%)", "hsl(180,60%,50%)"]) .interpolate(function(a, b) { var i = d3.interpolateString(a, b); return function(t) { return d3.hsl(i(t)); }; }); var arcBody = d3.svg.arc() .startAngle(0) .endAngle(function(d) { return d.value * 2 * Math.PI; }) .innerRadius(function(d) { return d.index * radius; }) .outerRadius(function(d) { return (d.index + spacing) * radius; }) .cornerRadius(6); var arcCenter = d3.svg.arc() .startAngle(0) .endAngle(function(d) { return d.value * 2 * Math.PI; }) .innerRadius(function(d) { return (d.index + spacing / 2) * radius; }) .outerRadius(function(d) { return (d.index + spacing / 2) * radius; }); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var field = svg.selectAll("g") .data(fields) .enter().append("g"); field.append("path") .attr("class", "arc-body"); field.append("path") .attr("id", function(d, i) { return "arc-center-" + i; }) .attr("class", "arc-center"); field.append("text") .attr("dy", ".35em") .attr("dx", ".75em") .style("text-anchor", "start") .append("textPath") .attr("startOffset", "50%") .attr("class", "arc-text") .attr("xlink:href", function(d, i) { return "#arc-center-" + i; }); tick(); d3.select(self.frameElement).style("height", height + "px"); function tick() { if (!document.hidden) field .each(function(d) { this._value = d.value; }) .data(fields) .each(function(d) { d.previousValue = this._value; }) .transition() .ease("elastic") .duration(500) .each(fieldTransition); setTimeout(tick, 1000 - Date.now() % 1000); } function fieldTransition() { var field = d3.select(this).transition(); field.select(".arc-body") .attrTween("d", arcTween(arcBody)) .style("fill", function(d) { return color(d.value); }); field.select(".arc-center") .attrTween("d", arcTween(arcCenter)); field.select(".arc-text") .text(function(d) { return d.text; }); } function arcTween(arc) { return function(d) { var i = d3.interpolateNumber(d.previousValue, d.value); return function(t) { d.value = i(t); return arc(d); }; }; } function fields() { var now = new Date; return [ {index: .7, text: formatSecond(now), value: now.getSeconds() / 60}, {index: .6, text: formatMinute(now), value: now.getMinutes() / 60}, {index: .5, text: formatHour(now), value: now.getHours() / 24}, {index: .3, text: formatDay(now), value: now.getDay() / 7}, {index: .2, text: formatDate(now), value: (now.getDate() - 1) / (32 - new Date(now.getYear(), now.getMonth(), 32).getDate())}, {index: .1, text: formatMonth(now), value: now.getMonth() / 12} ]; } </script> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <p align="center">请选择需要练习的题目类型</p> <p> <label> <div align="center"> <input type="radio" name="RadioGroup1" value="zhengshu.jsp" id="RadioGroup1_0" /> 整数的算数题练习</div> </label> <label> <div align="center"> <input type="radio" name="RadioGroup1" value="zhenfenshu.jsp" id="RadioGroup1_1" /> 真分数算数题练习</div> </label> <label> <div align="center"> <input type="radio" name="RadioGroup1" value="daikuohao.jsp" id="RadioGroup1_2" /> 带括号算数题练习 </div> <div align="center"></div> </p> <div align="center"> <input type="button" value="开始答题" onClick="check()" /> </div> </form> <script> function check(){ var input = document.getElementsByName("RadioGroup1"); for(var i=0; i< input.length; i++ ){ if(input[i].checked == true ){ //window.open(input[i].value,null); //新窗口打开 window.location=input[i].value; //当前窗口打开,注释一种方法 } } } </script> </body> </html>
zhengshu.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>整数</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <form id="form1" name="form1" method="post" action="chuanzhi.jsp"> <p> <label for="textfield"></label> </p> <p> </p> <p align="center"><br> 最小值:<input type="text" name="min" id="min" /> <br> 最大值:<input type="text" name="max" id="max" /> <br> 题目个数:<input type="text" name="geshu" id="geshu" /> <br> 选项:1有乘除法且除法无余数 2有乘除法余数不定 3无乘除法 <br> 选项: <input type="text" name="choose" id="choose" /> </p> <div align="center"> <input type="submit" value="确定" value="chuanzhi.jsp"> </div> </form> </body> </html>
chuanzhi.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.regex.Pattern, java.util.regex.Matcher"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>传值</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <% String min = request.getParameter("min"); session.setAttribute("min",min); String max = request.getParameter("max"); session.setAttribute("max",max); String choose = request.getParameter("choose"); session.setAttribute("choose",choose); String geshu = request.getParameter("geshu"); session.setAttribute("geshu",geshu); Pattern pattern = Pattern.compile("[0-9]*"); Pattern pattern1 = Pattern.compile("[1-3]"); Matcher isNum = pattern.matcher(min); Matcher isNum2 = pattern.matcher(max); Matcher isNum3 = pattern.matcher(geshu); Matcher isNum4 = pattern1.matcher(choose); if( (!isNum.matches())||(!isNum2.matches())||(!isNum3.matches())||(!isNum4.matches())) { out.println("<script language = ‘javascript‘> alert(‘输入错误,点击确定重新输入!!!‘);</script>"); response.setHeader("refresh","0;url = zhengshu.jsp"); } else { response.setHeader("refresh","0;url = zhengshushizi.jsp"); } %> </body> </html>
zhengshushizi.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>整数式子</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <div align="center"> <%! int i=0; String Array[]=new String[1000]; class Zhengshu { String re1=""; int re; String function(int r ,int q, int fun) { //1有乘除法 2加减无负数 3除法无余数(12,1,0) String fu =""; String shizi=""; int a=(int)(Math.random()*(r)%(r-q+1)+q); int b=(int)(Math.random()*(r)%(r-q+1)+q); int c=(int)(Math.random()*4); //1有乘除法 if(fun==1) { if(c==0) { fu="+"; shizi=a+" "+fu+" "+b; re=a+b; re1=String.valueOf(re); } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { fu="×"; shizi=a+" "+fu+" "+b; re=a*b; re1=String.valueOf(re); } if(c==3) { fu="÷"; //排除分母为0的可能性和余数的可能性 while(b==0||a%b!=0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a/b; shizi=a+" "+fu+" "+b; re1=String.valueOf(re); } } //2有乘除法余数不定 if(fun==2) { if(c==0) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { fu="×"; re=a*b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==3) { fu="÷"; //排除分母为0的可能性 while(b==0) { b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a/b; if(a%b!=0) { int flag1=a%b; re1=String.valueOf(re)+"..."+String.valueOf(flag1); } else re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } } //3无乘除法 if(fun==3) { if(c==0) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a-b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==2) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==3) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a-b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } } return shizi; } } %> <% Connection con = null; //定义一个mysql链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement(); int max=Integer.parseInt(String.valueOf(session.getAttribute("max"))); int min=Integer.parseInt(String.valueOf(session.getAttribute("min"))); int choose=Integer.parseInt(String.valueOf(session.getAttribute("choose"))); int geshu=Integer.parseInt(String.valueOf(session.getAttribute("geshu"))); session.setAttribute("geshu1", geshu); //String daan=request.getParameter("daan"); int zhengque1=0,cuowu1=0; int zhengque4[]=new int[geshu+10]; int cuowu4[]=new int[geshu+10]; Zhengshu a = new Zhengshu(); //for(int i=0;i<geshu;i++) %> <% if(i<geshu) { Array[i]=a.function(max,min,choose); if(i>0) { for(int z=0;z<i;z++) { if((Array[z].equals(Array[i]))) { Array[i]=a.function(max,min,choose); z=-1; } } } out.println(i+1+": "+Array[i]+" = "); %> <% //将题目和答案写入数据库表 String sql="INSERT INTO 四则运算题目2 (shizi,result) VALUES (‘"+Array[i]+" = "+"‘,‘"+a.re1+"‘)"; //out.println("<script language = ‘javaScript‘> alert(‘"+shi+"‘);</script>"); stmt.executeUpdate(sql); %> <br> </div> <form id="form1" name="form1" method="post" action="zhengshusjk.jsp"> <p> </p> <p align="center">答案: <input type="text" name="daan" id="daan" /> <% String shi3=Array[i]+" = "; session.setAttribute("shi",shi3); String rere3=a.re1; session.setAttribute("rere",rere3); //String daan3=request.getParameter("daan"); //session.setAttribute("daan",daan3); session.setAttribute("i2",i); i++; stmt.close(); con.close(); %> <input type="submit" value="下一个" > </p> </form> <div align="center"> <% //String daan3=request.getParameter("daan"); //daan3=request.getParameter("daan"); //session.setAttribute("daan",daan3); %> <% } else { i=0; %> </div> <form action="chakan.jsp" method="post" > <div align="center"> <% String correct=String.valueOf(session.getAttribute("correct1")); String wrong=String.valueOf(session.getAttribute("wrong1")); %><br></br> <br></br> <input type="submit" value="查看成绩" name="button" align="middle"> </div> </form> <div align="center"> <% } %> </div> </body> </html>
zhengshusjk.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>判断答案</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> <%! String fencorrect="",fenwrong="";%> <% String daan3=request.getParameter("daan"); String shi5=String.valueOf(session.getAttribute("shi")); int geshu5=Integer.parseInt(String.valueOf(session.getAttribute("fengeshu1"))); int i5=Integer.parseInt(String.valueOf(session.getAttribute("i2"))); int i6=i5+1; String rere5=String.valueOf(session.getAttribute("rere")); if(rere5.equals(daan3)) { //out.println("恭喜你,回答正确!!!"); fencorrect=fencorrect+i6+" "; out.println("<script language = ‘javaScript‘> alert(‘恭喜你,回答正确!!!‘);</script>"); response.setHeader("refresh","0;url = zhenfenshushizi.jsp"); } else { //out.println("回答错误!正确答案为:"+result); fenwrong=fenwrong+i6+" "; out.println("<script language = ‘javaScript‘> alert(‘回答错误!!!正确答案:"+rere5+"‘);</script>"); response.setHeader("refresh","0;url = zhenfenshushizi.jsp"); } String fencorrect2=fencorrect; String fenwrong2=fenwrong; if(i5==geshu5-1) { fencorrect="";fenwrong=""; } session.setAttribute("fencorrect1",fencorrect2); session.setAttribute("fenwrong1",fenwrong2); %> </body> </html>
chakan.jsp:
<%@ 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> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>成绩查看</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <% String correct=String.valueOf(session.getAttribute("correct1")); String wrong=String.valueOf(session.getAttribute("wrong1")); out.print("正确的题号:"+correct+" 错误的题号:"+wrong); out.println("<script language = ‘javaScript‘> alert(‘点击确定返回开始页面!‘);</script>"); response.setHeader("refresh","0;url = choose.jsp"); %> </body> </html>
zhenfenshu.jsp:
<%@ 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> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>真分数</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <form id="form1" name="form1" method="post" action="fenchuanzhi.jsp"> <p> <label for="textfield"></label> </p> <p> </p> <p align="right"> </p> <p align="center"> 分子分母的 最大值: <input type="text" name="fenmax" id="fenmax" /> <br> 题目个数:<input type="text" name="fengeshu" id="fengeshu" /> <br> 选项:1有乘除法 2无乘除法 <br> 选项: <input type="text" name="fenchoose" id="fenchoose" /> </p> <div align="center"> <input type="submit" value="确定" value="fenchuanzhi.jsp"> </div> </form> </body> </html>
fenchuanzhi.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.regex.Pattern, java.util.regex.Matcher"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>传值</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <% String fenmax = request.getParameter("fenmax"); session.setAttribute("fenmax",fenmax); String fenchoose = request.getParameter("fenchoose"); session.setAttribute("fenchoose",fenchoose); String fengeshu = request.getParameter("fengeshu"); session.setAttribute("fengeshu",fengeshu); Pattern pattern = Pattern.compile("[0-9]*"); Pattern pattern1 = Pattern.compile("[1-2]"); Matcher isNum = pattern.matcher(fenmax); Matcher isNum3 = pattern.matcher(fengeshu); Matcher isNum4 = pattern1.matcher(fenchoose); if( (!isNum.matches())||(!isNum3.matches())||(!isNum4.matches())) { out.println("<script language = ‘javaScript‘> alert(‘输入错误,点击确定重新输入!!!‘);</script>"); response.setHeader("refresh","0;url = zhenfenshu.jsp"); } else { response.setHeader("refresh","0;url = zhenfenshushizi.jsp"); } %> </body> </html>
zhenfenshushizi.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>真分数式子</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <div align="center"> <%! int feni=0; String fenArray[]=new String[1000]; class zhenfenshu { String refenshu; //****最大公约**** double getGcd(double m,double n){ while(n > 0){ double temp = m % n; m = n; n = temp; } return m; } //****最大公约**** //***最小公倍数*** double getLcm(double m,double n){ double gcd = getGcd(m,n); double result = m*n / gcd; return result; } //***最小公倍数*** String fun(int r ,int fun) { //1有乘除法2没有乘除法 String fu =""; String shizi=""; int a=(int)(Math.random()*(r+1)); int b=(int)(Math.random()*(r+1)); int c=(int)(Math.random()*(r+1)); int d=(int)(Math.random()*(r+1)); int e=(int)(Math.random()*4); //1有乘除法****1 if(fun==1) { if(e==0) { fu="+"; while(a>=b||c>=d||b==0||d==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } //约分 int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a+c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else if(a>gongbei) { int s=a%gongbei; refenshu=1+"’"+s+"/"+gongbei; } else refenshu=a+"/"+gongbei; } if(e==1) { fu="-"; double y=getLcm(b,d); double a1=y/b; double c1=y/d; double a2=a*a1; double c2=c*c1; while(a>=b||c>=d||b==0||d==0||a2<c2) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); y=getLcm(b,d); a1=y/b; c1=y/d; a2=a*a1; c2=c*c1; } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a-c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else refenshu=a+"/"+gongbei; } if(e==2) { fu="×"; while(a>=b||c>=d||b==0||d==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; a=a*c; b=b*d; int flag1=(int) getGcd(a,b); a=a/flag1;b=b/flag1; if(a==0) refenshu="0"; else refenshu=a+"/"+b; } if(e==3) { fu="÷"; while(a>=b||c>=d||b==0||d==0||c==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; a=a*d; b=b*c; int flag1=(int) getGcd(a,b); a=a/flag1;b=b/flag1; if(a==0) refenshu="0"; else if(a==b) refenshu="1"; else if(b==1) refenshu=a+""; else if(a>b) { int s=a%b; int s1=a/b; refenshu=s1+"’"+s+"/"+b; } else refenshu=a+"/"+b; } } //2没有有乘除法***2 if(fun==2) { if(e==0) { fu="+"; while(a>=b||c>=d||b==0||d==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a+c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else if(a>gongbei) { int s=a%gongbei; refenshu=1+"’"+s+"/"+gongbei; } else refenshu=a+"/"+gongbei; } if(e==1) { fu="-"; double y=getLcm(b,d); double a1=y/b; double c1=y/d; double a2=a*a1; double c2=c*c1; while(a>=b||c>=d||b==0||d==0||a2<c2) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); y=getLcm(b,d); a1=y/b; c1=y/d; a2=a*a1; c2=c*c1; } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a-c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else refenshu=a+"/"+gongbei; } if(e==2) { fu="+"; while(a>=b||c>=d||b==0||d==0) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a+c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else if(a>gongbei) { int s=a%gongbei; refenshu=1+"’"+s+"/"+gongbei; } else refenshu=a+"/"+gongbei; } if(e==3) { fu="-"; double y=getLcm(b,d); double a1=y/b; double c1=y/d; double a2=a*a1; double c2=c*c1; while(a>=b||c>=d||b==0||d==0||a2<c2) { a=(int)(Math.random()*(r+1)); b=(int)(Math.random()*(r+1)); c=(int)(Math.random()*(r+1)); d=(int)(Math.random()*(r+1)); y=getLcm(b,d); a1=y/b; c1=y/d; a2=a*a1; c2=c*c1; } int aa=(int) getGcd(a,b); int cc=(int) getGcd(c,d); a=a/aa; b=b/aa; c=c/cc; d=d/cc; shizi=a+"/"+b+" "+fu+" "+c+"/"+d; int gongbei=(int) getLcm(b,d); a=a*(gongbei/b); c=c*(gongbei/d); a=a-c; int t=(int) getGcd(a,gongbei); a=a/t; gongbei=gongbei/t; if(a==gongbei) refenshu="1"; else if(a==0) refenshu="0"; else refenshu=a+"/"+gongbei; } } return shizi; } } %> <% Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement(); int fenmax=Integer.parseInt(String.valueOf(session.getAttribute("fenmax"))); int fenchoose=Integer.parseInt(String.valueOf(session.getAttribute("fenchoose"))); int fengeshu=Integer.parseInt(String.valueOf(session.getAttribute("fengeshu"))); session.setAttribute("fengeshu1", fengeshu); //String daan=request.getParameter("daan"); int zhengque1=0,cuowu1=0; int zhengque4[]=new int[fengeshu+10]; int cuowu4[]=new int[fengeshu+10]; zhenfenshu a = new zhenfenshu(); //for(int i=0;i<geshu;i++) %> <% if(feni<fengeshu) { fenArray[feni]=a.fun(fenmax,fenchoose); if(feni>0) { for(int z=0;z<feni;z++) { if((fenArray[z].equals(fenArray[feni]))) { fenArray[feni]=a.fun(fenmax,fenchoose); z=-1; } } } out.println(feni+1+": "+fenArray[feni]+" = "); %> <% //将题目和答案写入数据库表 String sql="INSERT INTO 四则运算题目1 (shizi,result) VALUES (‘"+fenArray[feni]+" = "+"‘,‘"+a.refenshu+"‘)"; //out.println("<script language = ‘javaScript‘> alert(‘"+shi+"‘);</script>"); stmt.executeUpdate(sql); %> <br> </div> <form id="form1" name="form1" method="post" action="zhenfenshusjk.jsp"> <p> </p> <p align="center">答案: <input type="text" name="daan" id="daan" /> <% String shi3=fenArray[feni]+" = "; session.setAttribute("shi",shi3); String rere3=a.refenshu; session.setAttribute("rere",rere3); //String daan3=request.getParameter("daan"); //session.setAttribute("daan",daan3); session.setAttribute("i2",feni); feni++; stmt.close(); con.close(); %> <input type="submit" value="下一个" > </p> </form> <div align="center"> <% //String daan3=request.getParameter("daan"); //daan3=request.getParameter("daan"); //session.setAttribute("daan",daan3); %> <% } else { feni=0; %> </div> <form action="fenchakan.jsp" method="post" > <div align="center"> <% String correct=String.valueOf(session.getAttribute("correct1")); String wrong=String.valueOf(session.getAttribute("wrong1")); %><br></br> <br></br> <input type="submit" value="查看成绩" name="button" align="middle"> </div> </form> <div align="center"> <% } %> </div> </body> </html>
zhenfenshusjk.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>判断答案</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> <%! String fencorrect="",fenwrong="";%> <% String daan3=request.getParameter("daan"); String shi5=String.valueOf(session.getAttribute("shi")); int geshu5=Integer.parseInt(String.valueOf(session.getAttribute("fengeshu1"))); int i5=Integer.parseInt(String.valueOf(session.getAttribute("i2"))); int i6=i5+1; String rere5=String.valueOf(session.getAttribute("rere")); if(rere5.equals(daan3)) { //out.println("恭喜你,回答正确!!!"); fencorrect=fencorrect+i6+" "; out.println("<script language = ‘javaScript‘> alert(‘恭喜你,回答正确!!!‘);</script>"); response.setHeader("refresh","0;url = zhenfenshushizi.jsp"); } else { //out.println("回答错误!正确答案为:"+result); fenwrong=fenwrong+i6+" "; out.println("<script language = ‘javaScript‘> alert(‘回答错误!!!正确答案:"+rere5+"‘);</script>"); response.setHeader("refresh","0;url = zhenfenshushizi.jsp"); } String fencorrect2=fencorrect; String fenwrong2=fenwrong; if(i5==geshu5-1) { fencorrect="";fenwrong=""; } session.setAttribute("fencorrect1",fencorrect2); session.setAttribute("fenwrong1",fenwrong2); %> </body> </html>
fenchakan.jsp:
<%@ 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> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>成绩查看</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <% String fencorrect=String.valueOf(session.getAttribute("fencorrect1")); String fenwrong=String.valueOf(session.getAttribute("fenwrong1")); out.print("正确的题号:"+fencorrect+" 错误的题号:"+fenwrong); out.println("<script language = ‘javaScript‘> alert(‘点击确定返回开始页面!‘);</script>"); response.setHeader("refresh","0;url = choose.jsp"); %> </body> </html>
daikuohao.jsp:
<%@ 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> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>带括号</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <p> </p> <form id="form1" name="form1" method="post" action="kuohaochuanzhi.jsp"> <label for="textfield"></label> <div align="center">最小值: <input type="text" name="kuohaomin" id="kuohaomin" /> <br> 最大值:<input type="text" name="kuohaomax" id="kuohaomax" /> <br> 题目个数:<input type="text" name="kuohaogeshu" id="kuohaogeshu" /> <br> </div> <div align="center"> <input type="submit" value="确定" value="kuohaochuanzhi.jsp"> </div> </form> </body> </html>
kuohaochuanzhi.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.regex.Pattern, java.util.regex.Matcher"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>传值</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <% String kuohaomax = request.getParameter("kuohaomax"); session.setAttribute("kuohaomax10",kuohaomax); String kuohaomin = request.getParameter("kuohaomin"); session.setAttribute("kuohaomin10",kuohaomin); String kuohaogeshu = request.getParameter("kuohaogeshu"); session.setAttribute("kuohaogeshu10",kuohaogeshu); Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(kuohaomax); Matcher isNum2 = pattern.matcher(kuohaomin); Matcher isNum3 = pattern.matcher(kuohaogeshu); if((!isNum.matches())||(!isNum3.matches())||(!isNum2.matches())||kuohaomax.equals("")||kuohaomin.equals("")||kuohaogeshu.equals("")) { out.println("<script language = ‘javaScript‘> alert(‘输入错误,点击确定重新输入!!!‘);</script>"); response.setHeader("refresh","0;url = daikuohao.jsp"); } else { response.setHeader("refresh","0;url = daikuohaoshizi.jsp"); } %> </body> </html>
daikuohaoshizi.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>带括号式子</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <div align="center"> <%! int kuohaoi=0; String kuohaoArray[]=new String[1000]; class daikuohao { //保存整数结果 int re; //保存整数带余数的结果 String re1; //保存三个整数公式的结果 int resan; String function(int r ,int q, int fun) { //1有乘除法 2加减无负数 3除法无余数(12,1,0) String fu =""; String shizi=""; int a=(int)(Math.random()*(r)%(r-q+1)+q); int b=(int)(Math.random()*(r)%(r-q+1)+q); int c=(int)(Math.random()*4); //1乘除法 2除法无余数****12 if(fun==12) { if(c==0) { fu="+"; shizi=a+" "+fu+" "+b; re=a+b; re1=String.valueOf(re); } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { fu="×"; shizi=a+" "+fu+" "+b; re=a*b; re1=String.valueOf(re); } if(c==3) { fu="÷"; //排除分母为0的可能性和余数的可能性 while(b==0||a%b!=0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a/b; shizi=a+" "+fu+" "+b; re1=String.valueOf(re); } } //1乘除法 2除法无余数****1 if(fun==1) { if(c==0) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { fu="×"; re=a*b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==3) { fu="÷"; //排除分母为0的可能性 while(b==0) { b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a/b; if(a%b!=0) { int flag1=a%b; re1=String.valueOf(re)+"..."+String.valueOf(flag1); } else re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } } //1乘除法 2除法无余数****0 if(fun==0) { if(c==0) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a-b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==2) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==3) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a-b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } } return shizi; } //***************生成整数公式的函数********************** String sgongshi(int r,int q) { String shi,shizi=""; int a=(int)(Math.random()*(r)%(r-q+1)+q); int e=(int)(Math.random()*4); shi=function(r,q,12); if(e==0) { int f=(int)(Math.random()*4); if(f==1||f==0) { shizi=a+" + "+"( "+shi+" )"; resan=a+re; } if(f==2||f==3) { shizi="( "+shi+" )"+" + "+a; resan=a+re; } } if(e==1) { int f=(int)(Math.random()*4); if(f==1||f==0) { while(a-re<0) { shi=function(r,q,12); } shizi=a+" - "+"( "+shi+" )"; resan=a-re; } if(f==2||f==3) { while(a-re>0) { shi=function(r,q,12); } shizi="( "+shi+" )"+" - "+a; resan=re-a; } } if(e==2) { int f=(int)(Math.random()*4); if(f==1||f==0) { shizi=a+" × "+"( "+shi+" )"; resan=a*re; } if(f==2||f==3) { shizi="( "+shi+" )"+" × "+a; resan=a*re; } } if(e==3) { int f=(int)(Math.random()*4); if(f==1||f==0) { while(re==0) { shi=function(r,q,12); } while(a%re!=0) { shi=function(r,q,12); while(re==0) { shi=function(r,q,12); } } shizi=a+" ÷ "+"( "+shi+" )"; resan=a/re; } if(f==2||f==3) { while(a==0) { a=(int)(Math.random()*(r)%(r-q+1)+q); } while(re%a!=0) { shi=function(r,q,12); } shizi="( "+shi+" )"+" ÷ "+a; resan=re/a; } } return shizi; } //*****生成三个数的式子的函数****** } %> <% Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement(); int kuohaomax=Integer.parseInt(String.valueOf(session.getAttribute("kuohaomax10"))); int kuohaomin=Integer.parseInt(String.valueOf(session.getAttribute("kuohaomin10"))); int kuohaogeshu=Integer.parseInt(String.valueOf(session.getAttribute("kuohaogeshu10"))); session.setAttribute("kuohaogeshu1", kuohaogeshu); //String daan=request.getParameter("daan"); int zhengque1=0,cuowu1=0; int zhengque4[]=new int[kuohaogeshu+10]; int cuowu4[]=new int[kuohaogeshu+10]; daikuohao a = new daikuohao(); //for(int i=0;i<geshu;i++) %> <% if(kuohaoi<kuohaogeshu) { kuohaoArray[kuohaoi]=a.sgongshi(kuohaomax,kuohaomin); if(kuohaoi>0) { for(int z=0;z<kuohaoi;z++) { if((kuohaoArray[z].equals(kuohaoArray[kuohaoi]))) { kuohaoArray[kuohaoi]=a.sgongshi(kuohaomax,kuohaomin); z=-1; } } } out.println(kuohaoi+1+": "+kuohaoArray[kuohaoi]+" = "); %> <% //将题目和答案写入数据库表 String sql="INSERT INTO 四则运算题目3 (shizi,result) VALUES (‘"+kuohaoArray[kuohaoi]+" = "+"‘,‘"+a.resan+"‘)"; //out.println("<script language = ‘javaScript‘> alert(‘"+shi+"‘);</script>"); stmt.executeUpdate(sql); %> <br> </div> <form id="form1" name="form1" method="post" action="kuohaosjk.jsp"> <div align="center"> <p> </p> <p>答案: <input type="text" name="daan" id="daan" /> <% String shi3=kuohaoArray[kuohaoi]+" = "; session.setAttribute("shi",shi3); int rere3=a.resan; session.setAttribute("rere",rere3); //String daan3=request.getParameter("daan"); //session.setAttribute("daan",daan3); session.setAttribute("i2",kuohaoi); kuohaoi++; stmt.close(); con.close(); %> <input type="submit" value="下一个" > </p> </div> </form> <div align="center"> <% //String daan3=request.getParameter("daan"); //daan3=request.getParameter("daan"); //session.setAttribute("daan",daan3); %> <% } else { kuohaoi=0; %> </div> <form action="kuohaochakan.jsp" method="post" > <div align="center"> <% String correct=String.valueOf(session.getAttribute("correct1")); String wrong=String.valueOf(session.getAttribute("wrong1")); %><br></br> <br></br> <input type="submit" value="查看成绩" name="button" align="middle"> </div> </form> <div align="center"> <% } %> </div> </body> </html>
kuohaosjk.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>判断答案</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> <%! String kuohaocorrect="",kuohaowrong="";%> <% String daan3=request.getParameter("daan"); String shi5=String.valueOf(session.getAttribute("shi")); int geshu5=Integer.parseInt(String.valueOf(session.getAttribute("kuohaogeshu1"))); int i5=Integer.parseInt(String.valueOf(session.getAttribute("i2"))); int i6=i5+1; String rere5=String.valueOf(session.getAttribute("rere")); if(rere5.equals(daan3)) { //out.println("恭喜你,回答正确!!!"); kuohaocorrect=kuohaocorrect+i6+" "; out.println("<script language = ‘javaScript‘> alert(‘恭喜你,回答正确!!!‘);</script>"); response.setHeader("refresh","0;url = daikuohaoshizi.jsp"); } else { //out.println("回答错误!正确答案为:"+result); kuohaowrong=kuohaowrong+i6+" "; out.println("<script language = ‘javaScript‘> alert(‘回答错误!!!正确答案:"+rere5+"‘);</script>"); response.setHeader("refresh","0;url = daikuohaoshizi.jsp"); } String kuohaocorrect2=kuohaocorrect; String kuohaowrong2=kuohaowrong; if(i5==geshu5-1) { kuohaocorrect="";kuohaowrong=""; } session.setAttribute("kuohaocorrect1",kuohaocorrect2); session.setAttribute("kuohaowrong1",kuohaowrong2); %> </body> </html>
kuohaochakan.jsp:
<%@ 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> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>成绩查看</title> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(b.jpg); } </style> </head> <body> <% String kuohaocorrect=String.valueOf(session.getAttribute("kuohaocorrect1")); String kuohaowrong=String.valueOf(session.getAttribute("kuohaowrong1")); out.print("正确的题号:"+kuohaocorrect+" 错误的题号:"+kuohaowrong); out.println("<script language = ‘javaScript‘> alert(‘点击确定返回开始页面!‘);</script>"); response.setHeader("refresh","0;url = choose.jsp"); %> </body> </html>
结果截图:
以上是关于web版四则运算的主要内容,如果未能解决你的问题,请参考以下文章
[vscode]--HTML代码片段(基础版,reactvuejquery)
为啥我的 C 代码片段不起作用?简化版可以。为 unsigned long long 传递不带 VA_ARGS 的 args