四则运算网页
设计思路:
1.遍历每一个运算符,即在运算的题目答案的时候,使得不出现负数和假分数(若出现就调换数的位置或者改变运算符)
2.添加括号的方法就是列举所有加括号可能的情况,然后在相应的位置输出括号和进行计算
3.因为前面用的都是死方法,所以没有找到较好地方法检查重复
<%@ 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>Insert title here</title> </head> <body> <form method="post" action="2.jsp"> <center> <table> <tr> <td>是否有乘除: <select name="muldiv"> <option value=0>无乘除法</option> <option value=1>有乘除法</option> </td> </tr> <tr> <td>是否有括号: <select name="bra"> <option value=0>无括号</option> <option value=1>有括号</option> </td> </tr> <tr> <td>取值范围(下限):<input type="text" name="min"></td> </tr> <tr> <td>取值范围(上限):<input type="text" name="max"></td> </tr> <tr> <td>题目数量:<input type="text" name="subject"></td> </tr> <tr> <td><input type="submit" value="确定"></td> </tr> </table> </center> </form> </body> </html>
<%@ 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>Insert title here</title> </head> <body> <form action="3.jsp"> <% String user0=request.getParameter("muldiv"); //是否有乘除 int muldiv=Integer.valueOf(user0); String user1=request.getParameter("bra"); //是否有括号 int bra=Integer.valueOf(user1); String user2=request.getParameter("min"); //取值最小值 int min=Integer.valueOf(user2); String user3=request.getParameter("max"); //取值最大值 int max=Integer.valueOf(user3); String user4=request.getParameter("subject"); //题目数量 int subject=Integer.parseInt(user4); int save; int mol = 0; int den = 0; String correct[] = new String[subject]; String answer[] = new String[subject]; for(int x=0;x<subject;x++) { answer[x]="name"+x; int number = (int)(Math.random()*3+2); //随机生成参与运算的数的个数 int num[] = new int[number]; //储存进行运算的数 String ope[] = new String[number]; //储存运算符 //两个数运算 if(number==2) { //随机生成进行运算的数 for(int i=0;i<number;i++) { num[i] = (int)(Math.random()*(max-min+1)+min); } //随机生成运算符 for(int i=0;i<number-1;i++) { int j = (int)(Math.random()*4); //乘除法 if(muldiv==0) { if(j==2) { j = 0; } else if(j==3) { j = 1; } } if(j==0) { ope[i] = "+"; } else if(j==1) { ope[i] = "-"; } else if(j==2) { ope[i] = "*"; } else if(j==3) { ope[i] = "/"; } } ope[number-1] = "="; //不产生负数 if(ope[0]=="-") { if(num[0]<num[1]) { int change; change = num[0]; num[0] = num[1]; num[1] = change; } } //保证真分数 else if(ope[0]=="/") { if(num[0]>num[1]) { int change; change = num[0]; num[0] = num[1]; num[1] = change; } } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <input type="text" name="<%=answer[x]%>"> <br></br> <% //储存答案 if(ope[0]=="+") { save = num[0] + num[1]; correct[x] = (save+""); } else if(ope[0]=="-") { save = num[0] - num[1]; correct[x] = (save+""); } else if(ope[0]=="*") { save = num[0] * num[1]; correct[x] = (save+""); } else if(ope[0]=="/") { //约分 for(int i=num[0];i>0;i--) { if(num[0]%i==0&&num[1]%i==0) { mol = num[0] / i; den = num[1] / i; break; } } correct[x] = (mol+"/"+den); } } //三个数运算 else if(number==3) { //随机生成进行运算的数 for(int i=0;i<number;i++) { num[i] = (int)(Math.random()*(max-min+1)+min); } //随机生成运算符 for(int i=0;i<number-1;i++) { int j = (int)(Math.random()*4); //乘除法 if(muldiv==0) { if(j==2) { j = 0; } else if(j==3) { j = 1; } } if(j==0) { ope[i] = "+"; } else if(j==1) { ope[i] = "-"; } else if(j==2) { ope[i] = "*"; } else if(j==3) { ope[i] = "/"; } } ope[number-1] = "="; //加括号 if(bra==1) { int brackets = (int)(Math.random()*3); //括前两个 if(brackets==0) { int check = 0; //保证不产生负数和假分数 if(ope[0]=="+") { check = num[0] + num[1]; } else if(ope[0]=="-") { if(num[0]<num[1]) { int change; change = num[0]; num[0] = num[1]; num[1] = change; } check = num[0] - num[1]; } else if(ope[0]=="*") { check = num[0] * num[1]; } else if(ope[0]=="/") { if(num[0]>num[1]) { int change; change = num[0]; num[0] = num[1]; num[1] = change; } check = num[0] / num[1]; } if(ope[1]=="-") { if(check<num[2]) { //输出算式 out.print(num[2]+ope[1]+"("+num[0]+ope[0]+num[1]+")"+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="/") { //约分 for(int i=num[0];i>0;i--) { if(num[0]%i==0&&num[1]%i==0) { mol = num[0] / i; den = num[1] / i; break; } } mol = num[2] * den - mol; correct[x] = (mol+"/"+den); } else { save = num[2] - check; correct[x] = (save+""); } } else { //输出算式 out.print("("+num[0]+ope[0]+num[1]+")"+ope[1]+num[2]+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="/") { //约分 for(int i=num[0];i>0;i--) { if(num[0]%i==0&&num[1]%i==0) { mol = num[0] / i; den = num[1] / i; break; } } mol = mol - num[2] * den; correct[x] = (mol+"/"+den); } else { save = check - num[2]; correct[x] = (save+""); } } } else if(ope[1]=="/") { if(check>=num[2]) { //输出算式 out.print(num[2]+ope[1]+"("+num[0]+ope[0]+num[1]+")"+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="/") { mol = num[1] * num[2]; den = num[0]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { mol = num[2]; den = check; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } else { //输出算式 out.print("("+num[0]+ope[0]+num[1]+")"+ope[1]+num[2]+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="/") { mol = num[0] * num[2]; den = num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { mol = check; den = num[2]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } } else { //输出算式 out.print("("+num[0]+ope[0]+num[1]+")"+ope[1]+num[2]+ope[2]); if(ope[1]=="+") { if(ope[0]=="/") { den = num[1]; mol = num[2] * den + num[0]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { save = check + num[2]; correct[x] = (save+""); } } else { if(ope[0]=="/") { den = num[1]; mol = num[2] * num[0]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { save = check * num[2]; correct[x] = (save+""); } } } } //括后两个 else if(brackets==1) { int check = 0; //保证不产生负数和假分数 if(ope[1]=="+") { check = num[1] + num[2]; } else if(ope[1]=="-") { if(num[1]<num[2]) { int change; change = num[1]; num[1] = num[2]; num[2] = change; } check = num[1] - num[2]; } else if(ope[1]=="*") { check = num[1] * num[2]; } else if(ope[1]=="/") { if(num[1]>num[2]) { int change; change = num[1]; num[1] = num[2]; num[2] = change; } check = num[1] / num[2]; } if(ope[0]=="-") { if(check>num[0]) { //输出算式 out.print("("+num[1]+ope[1]+num[2]+")"+ope[0]+num[0]+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="/") { den = num[2]; mol = num[1] - num[0] * den; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { save = check - num[0]; correct[x] = (save+""); } } else { //输出算式 out.print(num[0]+ope[0]+"("+num[1]+ope[1]+num[2]+")"+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="/") { den = num[2]; mol = num[0] * den - num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { save = num[0] - check; correct[x] = (save+""); } } } else if(ope[0]=="/") { if(check<num[0]) { //输出算式 out.print("("+num[1]+ope[1]+num[2]+")"+ope[0]+num[0]+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="/") { den = num[2] * num[0]; mol = num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { den = num[0]; mol = check; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } else { //输出算式 out.print(num[0]+ope[0]+"("+num[1]+ope[1]+num[2]+")"+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="/") { mol = num[2] * num[0]; den = num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { mol = num[0]; den = check; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } } else { //输出算式 out.print(num[0]+ope[0]+"("+num[1]+ope[1]+num[2]+")"+ope[2]); %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="+") { if(ope[1]=="/") { den = num[2]; mol = num[1] + num[0] * den; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { save = num[0] + check; correct[x] = (save+""); } } else { if(ope[1]=="/") { den = num[2]; mol = num[0] * num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else { save = num[0] * check; correct[x] = (save+""); } } } } //没有括号 else { int check = 0; if(ope[0]=="+") { check = num[0] + num[1]; if(ope[1]=="+"||ope[1]=="*") { //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="+") { save = num[0] + num[1] + num[2]; correct[x] = (save+""); } else { save = num[0] + num[1] * num[2]; correct[x] = (save+""); } } else if(ope[1]=="-") { if(check<num[2]) { int change; change = num[1]; num[1] = num[2]; num[2] = change; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% save = num[0] + num[1] - num[2]; correct[x] = (save+""); } else if(ope[1]=="/") { if(num[1]>num[2]) { int change; change = num[1]; num[1] = num[2]; num[2] = change; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% den = num[2]; mol = num[0] * den + num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } else if(ope[0]=="-") { check = num[0] - num[1]; if(ope[1]=="+") { if(check<0) { int change; change = num[0]; num[0] = num[1]; num[1] = change; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% save = num[0] - num[1] + num[2]; correct[x] = (save+""); } else if(ope[1]=="-") { if(check<0) { int change; change = num[0]; num[0] = num[1]; num[1] = change; check = num[0] - num[1]; if(check<num[2]) { ope[1] = "+"; } } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="+") { save = num[0] - num[1] + num[2]; correct[x] = (save+""); } else { save = num[0] - num[1] - num[2]; correct[x] = (save+""); } } else if(ope[1]=="*") { check = num[1] * num[2]; if(num[0]<check) { int change1; change1 = num[0]; num[0] = num[2]; num[2] = change1; String change2; change2 = ope[0]; ope[0] = ope[1]; ope[1] = change2; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="-") { save = num[0] - num[1] * num[2]; correct[x] = (save+""); } else { save = num[0] * num[1] - num[2]; correct[x] = (save+""); } } else if(ope[1]=="/") { if(num[1]>num[2]) { int change; change = num[1]; num[1] = num[2]; num[2] = change; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% den = num[2]; mol = num[0] * den - num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } else if(ope[0]=="*") { check = num[0] * num[1]; if(ope[1]=="+"||ope[1]=="*") { //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="+") { save = num[0] * num[1] + num[2]; correct[x] = (save+""); } else { save = num[0] * num[1] * num[2]; correct[x] = (save+""); } } else if(ope[1]=="-") { if(check<num[2]) { int change1; change1 = num[0]; num[0] = num[2]; num[2] = change1; String change2; change2 = ope[0]; ope[0] = ope[1]; ope[1] = change2; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="-") { save = num[0] - num[1] * num[2]; correct[x] = (save+""); } else { save = num[0] * num[1] - num[2]; correct[x] = (save+""); } } else if(ope[1]=="/") { if(check>num[2]) { ope[1] = "*"; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="*") { save = num[0] * num[1] * num[2]; correct[x] = (save+""); } else { mol = check; den = num[2]; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } } else if(ope[0]=="/") { if(num[0]>num[1]) { int change; change = num[0]; num[0] = num[1]; num[1] = change; } if(ope[1]=="+"||ope[1]=="/"||ope[1]=="*") { //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="+") { den = num[1]; mol = num[0] + num[2] * den; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else if(ope[1]=="/") { den = num[1] * num[2]; mol = num[0]; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else if(ope[1]=="*") { den = num[1]; mol = num[0] * num[2]; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } else if(ope[1]=="-") { int change1; change1 = num[0]; num[0] = num[2]; num[2] = num[1]; num[1] = change1; String change2; change2 = ope[0]; ope[0] = ope[1]; ope[1] = change2; //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% den = num[2]; mol = num[0] * den - num[1]; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } } } //不加括号 else { int check = 0; if(ope[0]=="+") { check = num[0] + num[1]; if(ope[1]=="+"||ope[1]=="*") { //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="+") { save = num[0] + num[1] + num[2]; correct[x] = (save+""); } else { save = num[0] + num[1] * num[2]; correct[x] = (save+""); } } else if(ope[1]=="-") { if(check<num[2]) { int change; change = num[1]; num[1] = num[2]; num[2] = change; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% save = num[0] + num[1] - num[2]; correct[x] = (save+""); } else if(ope[1]=="/") { if(num[1]>num[2]) { int change; change = num[1]; num[1] = num[2]; num[2] = change; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% den = num[2]; mol = num[0] * den + num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } else if(ope[0]=="-") { check = num[0] - num[1]; if(ope[1]=="+") { if(check<0) { int change; change = num[0]; num[0] = num[1]; num[1] = change; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% save = num[0] - num[1] + num[2]; correct[x] = (save+""); } else if(ope[1]=="-") { if(check<0) { int change; change = num[0]; num[0] = num[1]; num[1] = change; check = num[0] - num[1]; if(check<num[2]) { ope[1] = "+"; } } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="+") { save = num[0] - num[1] + num[2]; correct[x] = (save+""); } else { save = num[0] - num[1] - num[2]; correct[x] = (save+""); } } else if(ope[1]=="*") { check = num[1] * num[2]; if(num[0]<check) { int change1; change1 = num[0]; num[0] = num[2]; num[2] = change1; String change2; change2 = ope[0]; ope[0] = ope[1]; ope[1] = change2; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="-") { save = num[0] - num[1] * num[2]; correct[x] = (save+""); } else { save = num[0] * num[1] - num[2]; correct[x] = (save+""); } } else if(ope[1]=="/") { if(num[1]>num[2]) { int change; change = num[1]; num[1] = num[2]; num[2] = change; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% den = num[2]; mol = num[0] * den - num[1]; //约分 for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } else if(ope[0]=="*") { check = num[0] * num[1]; if(ope[1]=="+"||ope[1]=="*") { //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="+") { save = num[0] * num[1] + num[2]; correct[x] = (save+""); } else { save = num[0] * num[1] * num[2]; correct[x] = (save+""); } } else if(ope[1]=="-") { if(check<num[2]) { int change1; change1 = num[0]; num[0] = num[2]; num[2] = change1; String change2; change2 = ope[0]; ope[0] = ope[1]; ope[1] = change2; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[0]=="-") { save = num[0] - num[1] * num[2]; correct[x] = (save+""); } else { save = num[0] * num[1] - num[2]; correct[x] = (save+""); } } else if(ope[1]=="/") { if(check>num[2]) { ope[1] = "*"; } //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="*") { save = num[0] * num[1] * num[2]; correct[x] = (save+""); } else { mol = check; den = num[2]; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } } else if(ope[0]=="/") { if(num[0]>num[1]) { int change; change = num[0]; num[0] = num[1]; num[1] = change; } if(ope[1]=="+"||ope[1]=="/"||ope[1]=="*") { //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% if(ope[1]=="+") { den = num[1]; mol = num[0] + num[2] * den; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else if(ope[1]=="/") { den = num[1] * num[2]; mol = num[0]; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } else if(ope[1]=="*") { den = num[1]; mol = num[0] * num[2]; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } else if(ope[1]=="-") { int change1; change1 = num[0]; num[0] = num[2]; num[2] = num[1]; num[1] = change1; String change2; change2 = ope[0]; ope[0] = ope[1]; ope[1] = change2; //输出算式 for(int i=0;i<number;i++) { out.print(num[i]+ope[i]); } %> <form action="3.jsp"> <input type="text" name="<%=answer[x]%>"> <br></br> <% den = num[2]; mol = num[0] * den - num[1]; for(int i=mol;i>0;i--) { if(mol%i==0&&den%i==0) { mol = mol / i; den = den / i; break; } } correct[x] = (mol+"/"+den); } } } } } %> <% session.setAttribute("v", correct); %> <input type="submit" value="确认"> </form> </body> </html>
<%@ 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>Insert title here</title> </head> <body> <% String[] correct = (String[])session.getAttribute("v"); String[] answer = new String[correct.length]; %> <table> <th>序号<th>正确答案<th>输入答案<th>判断 <% for(int x=0;x<correct.length;x++) { answer[x] = request.getParameter("name"+x); out.print(correct[x]); %> <tr> <td align="center"><%=x+1 %></td> <td align="center"><%=correct[x] %></td> <td align="center"><%=answer[x] %></td> <% if(answer[x].equals(correct[x])) { %> <td align="center">正确</td> <% } else { %> <td align="center">错误</td> <% } %> </tr> <% } %> </table> </body> </html>