题目
看完题目,决定写一个web版的程序,由于知识和时间欠缺,所以没有把要求全部做到,没有添加括号和多个数字运算,最后的输入答案也没有确定判断方式。我会继续完善的!!!
设计思路:
乘除法:根据页面传来的值,控制随机数范围,进而控制有无乘除
char [] fu = new char [4];//生成符号,控制有无乘除 fu[0] = '-'; fu[1] = '+'; fu[2] = '/'; fu[3] = '*'; if(cc) fh = fu[new Random().nextInt(4)]; else fh = fu[new Random().nextInt(2)];
括号:(暂未实现)
数值范围:通过下拉框确定数值位数,然后控制随机数范围,生成数字
if(wei == 1) { shu1 = new Random().nextInt(9) + 1; shu2 = new Random().nextInt(9) + 1; } else if(wei == 2) { shu1 = new Random().nextInt(99) + 1; shu2 = new Random().nextInt(99) + 1; } else { shu1 = new Random().nextInt(999) + 1; shu2 = new Random().nextInt(999) + 1; }
有无负数、余数:通过单选框的值,然后对结果的判断,重新生成算式
for(int j = 0;j < i;j++) { if((c1[i].getShu1() == c1[j].getShu1() && c1[j].getShu2() == c1[i].getShu2()&& c1[i].getFh() == c1[j].getFh()) ||(fs == false && c1[i].getResult() < 0)||(ys == false && !c1[i].isInteger())) { c1[i] = new Cal_zs(cc, w); j = 0; } } c2[i] = new Cal_zfs(cc, w); for(int k = 0;k < i;k++) { if((c2[i].getShu1() == c2[k].getShu1() && c2[k].getShu2() == c2[i].getShu2() && c2[i].getFh() == c2[k].getFh())||(!fs && c1[i].getResult() < 0)) { c2[i] = new Cal_zfs(cc, w); k = 0; } }
代码:
first.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>出题</title> <style type="text/css"> h2{color:red} </style> </head> <body> <h2 align="center">出题器</h2> <form name="form1" action="chuti.jsp" method="post"> <table align="center"> <tr> <td>题目类型:</td> <td> <select name="lei"> <option value="整数" selected>整数</option> <option value="真分数">真分数</option> <option value="混合">混合</option> </select> </td> </tr> <tr> <td>是否有乘除法:</td> <td> <input type="radio" name="cc" value="无" checked>无 <input type="radio" name="cc" value="有">有 </td> </tr> <tr> <td>是否有括号:</td> <td> <input type="radio" name="kh" value="无" checked>无 <input type="radio" name="kh" value="有">有 </td> </tr> <tr> <td>数值范围:</td> <td> <select name="wei"> <option value="一位数" selected>一位数</option> <option value="两位数">两位数</option> <option value="三位数">三位数</option> </select> </td> </tr> <tr> <td>加减有无负数:</td> <td> <input type="radio" name="fushu" value="无" checked>无 <input type="radio" name="fushu" value="有">有 </td> </tr> <tr> <td>除法有无余数:</td> <td> <input type="radio" name="yushu" value="无" checked>无 <input type="radio" name="yushu" value="有">有 </td> </tr> <tr> <td>题量</td> <td> <input type="text" name="num" value="1">不大于100题 </td> </tr> <tr> <td></td> <td><input type="submit" value="开始出题!"></td> </tr> </table> </form> </body> </html>
chuti.jsp:
<%@ page language="java" import="java.util.*, java.sql.*, calculat.shu" contentType="text/html; charset=gb2312" pageEncoding="UTF-8"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <fmt:requestEncoding value="gb2312"/> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> h2{color:red} .tb1{ border-left-style:solid; border-bottom-style:solid; border-top-style:solid; border-right-style:solid; border-width:2px; } </style> <script type="text/javascript"> var i = 0; var Color = new Array("#0000FF", "#99FF00", "#660033", "#CC66CC", "#FFFF33"); function change(){ if(i > Color.length - 1) i = 0; table1.style.borderColor = Color[i]; i++; setTimeout("change()", 500); } </script> <script type="text/javascript"> function Case(txt) { var x=document.getElementById("result").value; if(x == txt.value) { alert("正确!"); outline="none"; border="1 solid green"; } else { outline="none"; border="1 solid red"; } } </script> <title>出题</title> </head> <body onload="change()"> <h1 align="center">题目</h1> <table id="table1" align="center" class="tb1"> <tr> <td>题目类型:</td> <td>${param.lei}</td> <td> </td> <td>是否有乘除法:</td> <td>${param.cc}</td> </tr> <tr> <td>是否有括号:</td> <td>${param.kh}</td> <td> </td> <td>数值范围:</td> <td>${param.wei}</td> </tr> <tr> <td>加减有无负数:</td> <td>${param.fushu}</td> <td> </td> <td>除法有无余数:</td> <td>${param.yushu}</td> </tr> </table> <h2 align="center">请填写答案:</h2> <form name="form2" action="yanzheng.jsp" method="post"> <table align="center"> <tr> <th>题目</th> <th> </th> <th>答案</th> </tr> <jsp:useBean id="c" class="calculat.shu" scope="request"/> <% boolean cc = true;//有无乘除,并赋值 if(request.getParameter("cc").equals("无")) cc = false; int w; if(request.getParameter("wei").equals("一位数")) w = 1; else if(request.getParameter("wei").equals("两位数")) w = 2; else w = 3; boolean fs = true; if(request.getParameter("fushu").equals("无"))//负数 fs = false; boolean ys = true; if(request.getParameter("yushu").equals("无"))//余数 ys = false; //生成算式 c = new shu(cc, w, fs, ys); int N = Integer.parseInt(request.getParameter("num"));//出题数 c.setN(N); String str = request.getParameter("lei");//出题类型 c.setCc(cc); String s = ""; for(int i = 0;i < N;i++) { out.print("<tr><td>"); if(str.equals("整数")) { out.print(c.show1(i) + "</td>"); s = c.result1(i); } else if(str.equals("真分数")) { out.print(c.show2(i) + "</td>"); s = c.result2(i); } else { if((new Random().nextInt(99) + 1)%2 == 0) { out.print(c.show1(i) + "</td>"); s = c.result1(i); } else { out.print(c.show2(i) + "</td>"); s = c.result2(i); } } %> <td>=</td> <td><input id="result" type="text" onblur="Case(s)"></td> </tr> <%} %> </table> </form> </body> </html>
Cal_zs.java:
package calculat; import java.util.Random; public class Cal_zs{ public char fh; public int shu1; public int shu2; public int result; public int getResult() { return result; } public Cal_zs() {} public char getFh() { return fh; } public int getShu1() { return shu1; } public int getShu2() { return shu2; } public Cal_zs(boolean cc,int wei) { char [] fu = new char [4];//生成符号,控制有无乘除 fu[0] = '-'; fu[1] = '+'; fu[2] = '/'; fu[3] = '*'; if(cc) fh = fu[new Random().nextInt(4)]; else fh = fu[new Random().nextInt(2)]; if(wei == 1) { shu1 = new Random().nextInt(9) + 1; shu2 = new Random().nextInt(9) + 1; } else if(wei == 2) { shu1 = new Random().nextInt(99) + 1; shu2 = new Random().nextInt(99) + 1; } else { shu1 = new Random().nextInt(999) + 1; shu2 = new Random().nextInt(999) + 1; } if(fh == '+') result = shu1 + shu2; else if(fh == '-') result = shu1 - shu2; else if(fh == '*') result = shu1 * shu2; else result = shu1 / shu2; } public String show() { return shu1 + " " + fh + " " + shu2; } public boolean isInteger(){ double r, s1 = shu1, s2 = shu2; if(fh == '/') { r = s1 / s2; String str = r + ""; for(int i = 0;i < str.length();i++) { if(str.charAt(i) == '.' && i == str.length() - 2 && str.charAt(i + 1) == '0') return true; } return false; } else return true; } }
Cal_zfs.java:
package calculat; import java.util.Random; public class Cal_zfs{ public char fh; public String shu1; public String shu2; public String result; public char getFh() { return fh; } public String getResult() { return result; } public String getShu1() { return shu1; } public String getShu2() { return shu2; } public Cal_zfs() { } public Cal_zfs(boolean cc, int w) { int fzi1; int fmu1; int fzi2; int fmu2; char [] fu = new char [4];//生成符号,控制有无乘除 fu[0] = '-'; fu[1] = '+'; fu[2] = '/'; fu[3] = '*'; if(cc) fh = fu[new Random().nextInt(4)]; else fh = fu[new Random().nextInt(2)]; if(w == 1) { fmu1 = new Random().nextInt(9) + 1; fmu2 = new Random().nextInt(9) + 1; } else if(w == 2) { fmu1 = new Random().nextInt(99) + 1; fmu2 = new Random().nextInt(99) + 1; } else { fmu1 = new Random().nextInt(999) + 1; fmu2 = new Random().nextInt(999) + 1; } fzi1 = new Random().nextInt(fmu1) + 1; fzi2 = new Random().nextInt(fmu2) + 1; int gy = gys(fzi1, fmu1); int gy2 = gys(fzi2, fmu2); if(fzi1 == fmu1) shu1 = "1"; else shu1 = fzi1 / gy + "/" + fmu1 / gy; if(fzi2 == fmu2) shu1 = "1"; else shu2 = fzi2 / gy2 + "/" + fmu2 / gy2; int fm = 0, fz = 0; if(fh == '+') { fm = fmu1 * fmu2; fz = fmu1 * fzi2 + fmu2 * fzi1; } else if(fh == '-') { fm = fmu1 * fmu2; fz = fmu2 * fzi1 - fmu1 * fzi2; } else if(fh == '*') { fm = fmu1 * fmu2; fz = fzi1 * fzi2; } else { fm = fmu1 * fzi2; fz = fzi1 * fmu2; } int g = gys(fz, fm); if(fz == fm) result = "1"; else result = fz / g + "/" + fm / g; } public int gys(int s1, int s2) { int gy = 1; for(int i = 1; i <= (s1 < s2 ? s1 : s2); i++) { if(s1 % i == 0 && s2 % i == 0) gy = i; } return gy; } public String show() { return shu1 + " " + fh + " " + shu2; } }
shu.java:
package calculat; public class shu { private Cal_zs [] c1 = new Cal_zs [100]; private Cal_zfs [] c2 = new Cal_zfs [100]; boolean cc; int N; int wei; public void setN(int n) { N = n; } public void setwei(int w) { wei = w; } public void setCc(boolean cc) { this.cc = cc; } public shu() { } public shu(boolean cc, int w, boolean fs, boolean ys) { for(int i = 0;i < 100;i++) { c1[i] = new Cal_zs(cc, w); for(int j = 0;j < i;j++) { if((c1[i].getShu1() == c1[j].getShu1() && c1[j].getShu2() == c1[i].getShu2()&& c1[i].getFh() == c1[j].getFh()) ||(fs == false && c1[i].getResult() < 0)||(ys == false && !c1[i].isInteger())) { c1[i] = new Cal_zs(cc, w); j = 0; } } c2[i] = new Cal_zfs(cc, w); for(int k = 0;k < i;k++) { if((c2[i].getShu1() == c2[k].getShu1() && c2[k].getShu2() == c2[i].getShu2() && c2[i].getFh() == c2[k].getFh())||(!fs && c1[i].getResult() < 0)) { c2[i] = new Cal_zfs(cc, w); k = 0; } } } } public String show1(int i) { return c1[i].show(); } public String show2(int i) { return c2[i].show(); } public String result1(int i) { return c1[i].getResult() + ""; } public String result2(int i) { return c2[i].getResult() + ""; } }
运行效果图:
出题器
运行结果截图(未选择所有情况):
总结:在web上学会了很多,可是也知道自己还有许多要学的,继续学习吧!