四则运算原程序代码:
1 package szys; 2 import java.util.Random ; 3 import java.util.Scanner; 4 import java.lang.reflect.Array; 5 import java.util.LinkedList; 6 import java.util.List; 7 8 public class FourOperations1 9 { 10 public static void main(String[] ages) 11 { 12 while(1>0) 13 { 14 System.out.print(" 请选择要求:" 15 +" 是否有乘除法;(True/False)" 16 +" 是否有括号(最多可以支持十个数参与运算)(True/False);" 17 +" 数值范围(True(1~10)/False(1~100));" 18 +" 加减乘除有无负数(True/False);" 19 +" 除法有无余数(True/False);"); 20 Scanner sc = new Scanner(System.in); 21 Boolean[] a=new Boolean[5]; 22 for(int i=0;i<5;i++) 23 { 24 a[i]=sc.nextBoolean(); 25 } 26 System.out.println("请输入要出题的个数"); 27 Scanner N = new Scanner(System.in); 28 int index=N.nextInt(); 29 int right=0,wrong=0; 30 31 for(int ii=0;ii<index;ii++) 32 { 33 if(a[1]==false) 34 { 35 36 int a1,a2,sign; 37 a1=RandomNum(100); 38 a2=RandomNum(100); 39 sign=RandomNum(4); 40 41 /**按照用户规定的各种要求对数据进行修改使数据合乎规范**/ 42 if(a[0]==false){ 43 sign=sign%2; 44 } 45 46 if(a[3]==true&&(sign==0||sign==1)){ 47 int ssign=RandomNum(2); 48 if(ssign==0){ 49 a1=-a1; 50 } 51 int ssign2=RandomNum(2); 52 if(sign==0){ 53 a2=-a2; 54 } 55 } 56 if(a[2]==true){ 57 a1=a1%10; 58 a2=a2%10; 59 } 60 if(sign==3&&a[4]==false){ 61 a1=a1*a2; 62 } 63 //无效算式被取消 64 if(sign==3&&a2==0){ 65 ii--; 66 } 67 else{ 68 System.out.print(a1); 69 PrintSign(sign); 70 System.out.print(a2+" = "); 71 int jjudge,result; 72 if(sign==0){ 73 result=a1+a2; 74 } 75 else if(sign==1){ 76 result=a1-a2; 77 } 78 else if(sign==2){ 79 result=a1*a2; 80 } 81 else{ 82 result=a1/a2; 83 } 84 jjudge=judge(result); 85 if(jjudge==0){ 86 right++; 87 } 88 if(jjudge==1){ 89 wrong++; 90 } 91 } 92 } 93 else if(a[1]==true) 94 { 95 int size=RandomNum(9); 96 int[] array=new int[size*2+4]; 97 /**二叉树非叶子结点的取值,即四则运算运算符的随机选择**/ 98 if(a[0]==false)//条件 没有乘除法 99 { 100 for(int i=0;i<=size;i++){ 101 array[i]=RandomNum(2); 102 } 103 } 104 else { 105 for(int i=0;i<=size;i++){ 106 array[i]=RandomNum(4); 107 } 108 } 109 /**二叉树叶子结点的取值,即四则运算运算数的随机取值**/ 110 if(a[2]==true)//数的取值范围为0~10 111 { 112 for(int i=size+1;i<=size*2+2;i++) 113 { 114 array[i]=RandomNum(6)+4;//防止取值为0~3 打印时显示成运算符 115 } 116 } 117 else{ 118 for(int i=size+1;i<=size*2+2;i++){ 119 120 array[i]=RandomNum(96)+4; 121 } 122 } 123 //限制条件:运算数随机选择是否为负数 124 if(a[3]==true){ 125 for(int i=size+1;i<=size*2+2;i++){ 126 int ssign=RandomNum(2); 127 if((array[i/2-1]==0||array[i/2-1]==1)&&ssign==0){ 128 array[i]=-array[i]; 129 } 130 } 131 } 132 /**将数组结点构建成二叉树**/ 133 LinkedList<Node> nodeList = new LinkedList<Node>(); 134 for(int nodeIndex=0;nodeIndex<array.length;nodeIndex++){ 135 nodeList.add(new Node(array[nodeIndex])); 136 } 137 creatBinTree(array,nodeList); 138 Node root = nodeList.get(0); 139 inOrderTraverse(root); 140 System.out.println(" = "); 141 values(root); 142 int jjudge=judge(root.result); 143 144 if(jjudge==0){ 145 right++; 146 } 147 if(jjudge==1){ 148 wrong++; 149 } 150 } 151 } 152 System.out.println("正确题数:"+right+"错误题数:"+wrong); 153 } 154 } 155 156 /**取要求范围内的随机数**/ 157 public static int RandomNum(int i) 158 { 159 Random a=new Random(); 160 int a1=a.nextInt (i); 161 return a1; 162 } 163 164 /**打印运算符**/ 165 public static void PrintSign(int sign) 166 { 167 if(sign==0){ 168 System.out.print(" + "); 169 } 170 else if(sign==1){ 171 System.out.print(" - "); 172 } 173 else if(sign==2){ 174 System.out.print(" * "); 175 } 176 else if(sign==3){ 177 System.out.print(" / "); 178 } 179 else 180 System.out.print(sign); 181 } 182 183 /**定义二叉树结构**/ 184 public static class Node 185 { 186 Node leftchild; 187 Node rightchild; 188 int data; 189 int result;//各二叉树分支之下的四则运算结果 190 191 Node(int newData){ 192 leftchild=null; 193 rightchild=null; 194 data=newData; 195 result=data; 196 } 197 } 198 /**构建二叉树**/ 199 public static void creatBinTree(int array[],LinkedList<Node> nodeList){ 200 201 for(int parentIndex=0;parentIndex<array.length/2-1;parentIndex++){ 202 nodeList.get(parentIndex).leftchild = nodeList.get(parentIndex * 2 + 1); 203 nodeList.get(parentIndex).rightchild = nodeList.get(parentIndex * 2 + 2); 204 } 205 } 206 /**中序遍历二叉树 即打印四则运算算式**/ 207 public static void inOrderTraverse(Node node) { 208 if (node == null) 209 return; 210 if (node.leftchild != null) { 211 System.out.print("("); 212 } 213 inOrderTraverse(node.leftchild); 214 PrintSign(node.data ); 215 inOrderTraverse(node.rightchild); 216 if (node.rightchild != null) { 217 System.out.print(")"); 218 } 219 } 220 /**计算四则运算的值,和括号内每一步运算的值 采用递归算法**/ 221 public static void values(Node node){ 222 if (node.leftchild== null) { 223 return; 224 } 225 values(node.leftchild); 226 values(node.rightchild); 227 if(node.data==0){ 228 node.result=node.leftchild.result+node.rightchild.result; 229 } 230 else if(node.data==1){ 231 node.result=node.leftchild.result-node.rightchild.result; 232 } 233 else if(node.data==2){ 234 node.result=node.leftchild.result*node.rightchild.result; 235 } 236 else 237 { 238 if(node.rightchild.result==0){ 239 System.out.println("被除数为零,该算式无效!"); 240 return; 241 } 242 node.result=node.leftchild.result/node.rightchild.result; 243 } 244 } 245 /**判断用户输入答案是否正确**/ 246 public static int judge(int answer){ 247 System.out.println(" 请输入您的答案: "); 248 Scanner sc2 = new Scanner(System.in); 249 int userResult=sc2.nextInt(); 250 System.out.println(" 正确答案是:"+answer ); 251 if(userResult==answer){ 252 return 0; 253 } 254 else { 255 return 1; 256 } 257 } 258 }
网页版:
需求选择界面代码:
<%@ 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> </head> <body> <h1>小学四则运算</h1> <form action="main1.jsp" method="post"> <p>是否有乘除法 <input type="radio" name="chengchu" value=0 >yes <input type="radio" name="chengchu" value=1>no <p>是否有括号 <input type="radio" name="kuohao" value=0 >yes <input type="radio" name="kuohao" value=1>no <p>数值范围 <input type="radio" name="fanwei" value=0 >0~10 <input type="radio" name="fanwei" value=1 checked>0~100 <p>加减有无负数 <input type="radio" name="fushu" value=0 >yes <input type="radio" name="fushu" value=1>no <p>除法有无余数 <input type="radio" name="yushu" value=0>yes <input type="radio" name="yushu" value=1>no <P>数量 <input type="text" name="num"> <br> <input type="submit" value="答题"> <input type="reset" value="清除"> </form> </body> </html>
运行效果图:
出题界面源代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Random "%> <!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 > <%! public static int RandomNum(int i) { Random a=new Random(); int a1=a.nextInt (i); return a1; } %> <% String snum ; String choice[]=new String[5]; int inum; Boolean choice1[]=new Boolean[5]; int result; %> <% snum=request.getParameter("num"); try{ inum=Integer.valueOf(snum); }catch(NumberFormatException e){ out.println("出题数量格式不正确!"); inum=new Integer(0); } choice[0]=request.getParameter("chengchu"); if(choice[0]==null){ out.println("没有选择是否有乘除法"); } else{ if(choice[0].equals("0")){ choice1[0]=true; } if(choice[0].equals("1")){ choice1[0]=false; } } choice[1]=request.getParameter("kuohao"); if(choice[1]==null){ out.println("没有选择是否有括号"); } else{ if(choice[1].equals("0")){ choice1[1]=true; } if(choice[1].equals("1")){ choice1[1]=false; } } choice[2]=request.getParameter("fanwei"); if(choice[2]==null){ out.println("没有选择数值范围"); } else{ if(choice[2].equals("0")){ choice1[2]=true; } if(choice[2].equals("1")){ choice1[2]=false; } } choice[3]=request.getParameter("fushu"); if(choice[3]==null){ out.println("没有选择是否有负数"); } else{ if(choice[3].equals("0")){ choice1[3]=true; } if(choice[3].equals("1")){ choice1[3]=false; } } choice[4]=request.getParameter("yushu"); if(choice[4]==null){ out.println("没有选择是否有余数"); } else{ if(choice[4].equals("0")){ choice1[4]=true; } if(choice[4].equals("1")){ choice1[4]=false; } } %> <% if(choice[0]==null||choice[1]==null||choice[2]==null||choice[3]==null||choice[4]==null||inum==0){ } else{ int a1,a2,sign; a1=RandomNum(100); a2=RandomNum(100); sign=RandomNum(4); if(choice1[0]==false){ sign=sign%2; } if(choice1[3]==true&&(sign==0||sign==1)){ int ssign=RandomNum(2); if(ssign==0){ a1=-a1; } int ssign2=RandomNum(2); if(sign==0){ a2=-a2; } } if(choice1[2]==true){ a1=a1%10; a2=a2%10; } if(sign==3&&choice1[4]==false){ a1=a1*a2; } out.print(a1+" "); if(sign==0){ out.print(" + "); result=a1+a2; } else if(sign==1){ out.print(" - "); result=a1-a2; } else if(sign==2){ out.print(" * "); result=a1*a2; } else if(sign==3){ out.print(" / "); result=a1/a2; } out.print(a2+" = "); %> <form action="main.jsp"> 请输入答案<input id="answer" type="text" name="answer"> <button type="submit" >查看答案</button> </form> <br> <% } %> <a class="btn" href="demand.jsp"> 返回 </a> </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>答题</title> </head> <body> <script src='main1.js'><script> <% String a; a=request.getParameter("demo"); %> <script type="text/javascript"> function judge(){ try{ var x=document.getElementById("answer").value; if(x=="")throw "不能为空"; if(isNaN(x))throw "不是数字"; } catch(err) { var y=document.getElementById("mess"); y.innerHTML="答案格式不正确!"; } } function judge1(a,b){ if(a==b){ document.getElementById("mess2").innerHTML="回答正确"; } else{ document.getElementById("mess2").innerHTML="回答错误"; } } </script> <p id="mess"></p> <p id="mess2"></p> <script type="text/javascript"> judge(); judge1(result,a); </script> <div> <a class="btn" href="demand.jsp"> 返回 </a> </div> </body> </html>
运行效果图:
心得感想:
深刻得认识到自己的不足,应该在以后的学习中多多学习动态网页制作的知识,提升自己