四则运算2

通过对上次代码的完善,这次实现了参与运算变量的个数,将随机生成的数保存于数组中;对数值范围的操作,使随机生成的数在此范围内;也实现了对乘除法的控制,只是未实现有无括号和对结果的分析。

开始界面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>四则运算</title>
 8 </head>
 9 <body>
10    <h1>四则运算</h1>
11     <form action="yunsuan.jsp" method="post">
12         出题页数:<input type="text" name = "shuliang"><br>
13     <h2>1.整数运算  2.真分数运算</h2>
14     题目样式<input type="text" name="choose"><br><br>
15   数值范围<input type="text" name="xiao"> 
16         <input type="text" name="da"><br><br>
17     变量个数<input type="text" name="geshu"><br>
18     <h3>1.有乘除法  2.无乘除法</h3>
19     乘除法<input type="text" name="chengchu"><br>
20    <input type="submit" value="提交">
21    </form>
22 </body>
23 </html>

实现代码:

  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2     pageEncoding="UTF-8"%>
  3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4 <html>
  5 <head>
  6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7 <title>Insert title here</title>
  8 </head>
  9 <body>
 10 <%
 11 JspWriter mout=pageContext.getOut();
 12 String zh = request.getParameter("shuliang");
 13 String zhh = request.getParameter("choose");
 14 String Min=request.getParameter("xiao");
 15 String Max=request.getParameter("da");
 16 String shu = request.getParameter("geshu");
 17 String chu = request.getParameter("chengchu");
 18 int x=Integer.parseInt(zh);
 19 int y=Integer.parseInt(zhh);
 20 int min=Integer.parseInt(Min);
 21 int max=Integer.parseInt(Max);
 22 int z=Integer.parseInt(shu);
 23 int xx=Integer.parseInt(chu);
 24 int m,n,j,jj;
 25 char []ch=new char[4];
 26 ch[0]='+';
 27 ch[1]='-';
 28 ch[2]='*';
 29 ch[3]='/';
 30 if(y==1)
 31 {
 32     int []M=new int[10];
 33     for(int ii=1;ii<=x;ii++)
 34     {
 35         mout.print("30道题目");
 36         mout.print("<br>");
 37         String []A=new String[30];
 38         for(int i=0;i<30;i++)
 39         {
 40             A[i]="";
 41             for(int o=0;o<z;o++)
 42             {
 43                 M[o]=(int)(Math.random()*max+min);
 44                 if(xx==1)
 45                     j=(int)(Math.random()*4+1);
 46                 else
 47                     j=(int)(Math.random()*2+1);
 48                 if(o==z-1)
 49                     A[i]=A[i]+M[o]+" =";
 50                 else
 51                     A[i]=A[i]+M[o]+" "+ch[j-1];
 52             }
 53             for(int k=0;k<i;k++)
 54             {
 55                 if(A[i].equals(A[k]))
 56                 {
 57                     i--;
 58                     break;
 59                 }
 60             }
 61         }
 62         for(int i=0;i<30;i++)
 63         {
 64             mout.print((i+1)+". "+A[i]);
 65             mout.print("<br>");
 66         }
 67         mout.print("-"+ii+"-");
 68         mout.print("<br><br><br>");
 69     }
 70 }
 71 if(y==2)
 72 {
 73     for(int ii=1;ii<=x;ii++)
 74     {
 75         int a,b,c,d;
 76         String aa,bb;
 77         mout.print("30道题目");
 78         mout.print("<br>");
 79         String []B=new String[30];
 80         for(int i=0;i<30;i++)
 81         {
 82             a=(int)(Math.random()*100+1);
 83             b=(int)(Math.random()*100+1);
 84             if(a>b)
 85                 aa=b+"/"+a;
 86             else if(a<b)
 87                 aa=a+"/"+b;
 88             else
 89                 aa="1";
 90             c=(int)(Math.random()*100+1);
 91             d=(int)(Math.random()*100+1);
 92             if(c>d)
 93                 bb=d+"/"+c;
 94             else if(c<d)
 95                 bb=c+"/"+d;
 96             else
 97                 bb="1";
 98             j=(int)(Math.random()*4+1);
 99             B[i]=aa+" "+ch[j-1]+" "+bb+"=";
100             for(int k=0;k<i;k++)
101             {
102                 if(B[i].equals(B[k]))
103                 {
104                     i--;
105                     break;
106                 }
107             }
108         }
109         for(int i=0;i<30;i++)
110         {
111             mout.print((i+1)+". "+B[i]);
112             mout.print("<br>");
113         }
114         mout.print("-"+ii+"-");
115         mout.print("<br><br><br>");
116     mout.print("-"+ii+"-");
117     mout.print("<br><br><br>");
118     }
119 }
120 %>
121 </body>
122 </html>

 截图:

原文地址:https://www.cnblogs.com/jokerr/p/6532839.html