四则运算-进度三

 

  核心要求:(1)定义参数,控制题数和运算的数值范围,

         (2)不产生负数,当做除法时,如果不能整除,就写成真分数的形式。

         (3)将生成的题目和运算结果输出到数据库中

其中比较难的是数据库的链接部分,因为目前还没有学习:

 1 static void connect(String []r1,String[]r2,int num) throws ClassNotFoundException, SQLException
 2         {
 3             Connection conn=null;
 4             PreparedStatement pstmt = null;
 5             ResultSet rs = null;//定义好链接,执行,和结果
 6             String driverName = "com.mysql.jdbc.Driver";
 7             String userName = "root";//数据库的名
 8             String userPwd = "199762";//数据库密码
 9             String dbName = "wzw1";//数据库的名字
10             String url1 = "jdbc:mysql://localhost:3306/" + dbName;
11             String url2 = "?user=" + userName + "&password=" + userPwd;
12             String url3 = "&useUnicode=true&characterEncoding=utf-8";//规定好字符集
13             String url = url1 + url2 + url3;
14             Class.forName(driverName);//连接数据库
15             conn = DriverManager.getConnection(url);
16             String sql="insert into wzw3(titleexception,result) value(?,?)";
17             pstmt=conn.prepareStatement(sql);
18             
19             for(int i=0;i<num;i++)
20             {
21                 pstmt.setString(1, r1[i]);
22                 pstmt.setString(2,r2[i]);
23                 pstmt.executeUpdate();
24             }
25             if(pstmt != null) {
26                 pstmt.close();
27             }
28             
29             if(conn != null) {
30                 System.out.println("导入成功");
31                 conn.close();
32             }
33         }    
34         
View Code

 

还有就是产生多个真分数,并且加上括号。

一般有多少个数就创建数组长度为几,但是真分数需要创建的数组长度需要是数的2倍,这跟产生的括号的数组长度不一样,在产生表达式的时候放到一个for循环中就不太方便。

  1 static String properFractionExithk(int num,int scope)//产生含括号含有真分数的表达式
  2         {
  3             int []r1=new int[2*num];//接受产生的数值
  4             int []r2=new int[num-1];//接受符号
  5             String[]r3={"+","-","*","/"};
  6             int []r4=new int[num];//接受随机产生的括号
  7             String []r5=new String[2*num];//将接受的括号的个数,转成字符串
  8             String rs="";//接受含括号的和不含括号的表达式
  9             r4=chansheng(num);
 10             char ch='z';
 11             
 12             //产生数值
 13             while(ch=='z')
 14             {
 15                 
 16                 for(int i=0;i<2*num;i++)
 17                 {
 18                     r1[i]=(int) (Math.random()*(scope-1)+1);
 19                 }
 20                 ch='y';
 21                 int j=0;
 22                 while(j<2*num)
 23                 {
 24                     if(r1[j]>=r1[j+1])
 25                     {
 26                         ch='z';
 27                         break;
 28                     }
 29                     j++;
 30                     j++;
 31                 }
 32             }
 33             
 34             
 35             //产生符号
 36             for(int i=0;i<num-1;i++)
 37             {
 38                 r2[i]=(int) (Math.random()*4);
 39             }
 40             
 41             
 42             //产生括号的数组
 43             for(int i=0;i<2*num;i++)
 44             {
 45                 r5[i]="";
 46                 if(i%2==0)
 47                 {
 48                     if(r4[i/2]>0)
 49                     {
 50                         for(int j=0;j<r4[i/2];j++)
 51                         {
 52                             r5[i]+="(";
 53                         }
 54                     }
 55                     else if(r4[i/2]<0)
 56                     {
 57                         for(int j=0;j<0-r4[i/2];j++)
 58                         {
 59                             r5[i]+=")";
 60                         }
 61                     }
 62                 }
 63             }
 64             
 65             //添加到一个String类型的表达式中
 66             int j=0;
 67             while(j<2*num-2)
 68             {
 69                 int commondivisor=maxyue(r1[j], r1[j+1]);
 70                 r1[j]/=commondivisor;
 71                 r1[j+1]/=commondivisor;
 72                     if(r5[j].equals(""))
 73                     {
 74                         rs+=" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
 75                     }
 76                     else if(r5[j].substring(0, 1).equals("("))
 77                     {
 78                         rs+=r5[j]+" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
 79                     }
 80                     else
 81                     {
 82                         rs+=" "+r1[j]+"/"+r1[j+1]+" "+r5[j]+r3[r2[(j+1)/2]];
 83                     }
 84                 
 85                 j++;
 86                 j++;
 87             }
 88             //算最后一个数
 89             int commondivisor1=maxyue(r1[2*num-2],r1[2*num-1]);
 90             r1[2*num-2]/=commondivisor1;
 91             r1[2*num-1]/=commondivisor1;
 92             
 93                 if(r5[2*num-2].equals(""))
 94                 {
 95                     rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
 96                 }
 97                 else if(r5[2*num-2].substring(0, 1).equals("("))
 98                 {
 99                     rs+=r5[2*num-2]+" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
100                 }
101                 else
102                 {
103                     rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" "+r5[2*num-2];
104                 }
105             return rs;
106         }
107         
View Code

 全部代码:

  1 package Demo;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.util.Random;
  9 import java.util.Scanner;
 10 import java.util.Stack;
 11 
 12 public class arithmeticthree {
 13         static Scanner sc=new Scanner(System.in);
 14         public static void main(String[]args) throws ClassNotFoundException, SQLException
 15         {
 16             menu();
 17         }
 18 
 19         private static void menu() throws ClassNotFoundException, SQLException {
 20             int rs[]=new int[2];
 21             rs[0]=0;
 22             rs[1]=0;
 23             System.out.println("请选择题目类型:1.真分数。2整数");
 24             int choose1=sc.nextInt();
 25             System.out.println("请输入产生题的个数");
 26             int num=sc.nextInt();
 27             System.out.println("请输入取值范围");
 28             int scope=sc.nextInt();
 29             System.out.println("请选择题目类型中是否有括号:1.有。2.没有");
 30             int choose2=sc.nextInt();
 31             rs=operationAndStatistical(choose1, choose2, num,scope);
 32         }
 33         
 34         
 35         //真分数和整数都需要用到
 36         static int maxyue(int y,int x)//最大公约数
 37         {
 38             int r=y;
 39             while(r!=0)
 40             {
 41                 r=x%y;
 42                 x=y;
 43                 y=r;
 44             }
 45             return x;
 46         }
 47         static char youxian(String f,String s)//计算两个符号的优先级
 48         {
 49             char a1[][]={
 50                     {'>','>','<','<','<','>','>'},
 51                     {'>','>','<','<','<','>','>'},
 52                     {'>','>','>','>','<','>','>'},
 53                     {'>','>','>','>','<','>','>'},
 54                     {'<','<','<','<','<','=',' '},
 55                     {'>','>','>','>',' ','>','>'},
 56                     {'<','<','<','<','<',' ','='}
 57             };
 58             String a="+-*/()#";
 59             int a11=a.indexOf(f);
 60             int a12=a.indexOf(s);
 61             return a1[a11][a12];
 62         }
 63         private static int [] chansheng(int num)//随机产生括号
 64         {
 65             int []b=new int[num];
 66             for(int i=0;i<b.length;i++)
 67             {
 68                 b[i]=0;
 69             }
 70             Random rd=new Random();
 71             for(int i=2;i<num;i++)
 72             {
 73                 for(int j=0;j<num-i+1;j++)
 74                 {
 75                     int t=rd.nextInt(2);
 76                     if(t==1)
 77                     {
 78                         if(b[j]>=0&&b[j+i-1]<=0)
 79                         {
 80                             int c=0;
 81                             for(int k=j;k<j+i;k++)
 82                             {
 83                                 c+=b[k];
 84                             }
 85                             if(c==0)
 86                             {
 87                                 b[j]++;
 88                                 b[j+i-1]--;
 89                             }
 90                         }
 91                         
 92                     }
 93                 }
 94             }
 95             return b;
 96         }
 97             //运算
 98         static String jisuanbh(String a)//表达式的运算
 99         {
100             Stack <String>num=new Stack <String>();
101             Stack <String>fuhao=new Stack<String>();
102             a+="#";
103             fuhao.push("#");
104             char ch;
105             int i=0;
106             int s=0;
107             int y=0;
108             ch=a.charAt(i);
109             while(!(ch+"").equals("#") || !fuhao.peek().equals("#"))
110             {
111                 if(ch==' ')//如果遇到字符为空,说明遇到数字
112                 {
113                     String rn="";//用来记录数据
114                     while(true)
115                     {
116                         ch=a.charAt(++i);
117                         if(ch==' ')
118                         {
119                             break;
120                         }
121                         rn+=ch;
122                     }
123                     if((i+1)<a.length()){
124                         ch=a.charAt(++i);
125                     }
126                     num.push(rn);
127                 }
128                 else//遇到的是字符
129                 {
130                     char comp=youxian(fuhao.peek(),ch+"");//比较两个字符的优先级
131                     if(comp=='='){//说明遇到右括号
132                         fuhao.pop();
133                         if((i+1)<a.length()){
134                             ch=a.charAt(++i);
135                         }
136                     }
137                     else if(comp=='>')//优先级高,弹出两个数和一个运算符,进行运算
138                     {
139                         String st1=num.pop();
140                         String st2=num.pop();
141                         String fuh1=fuhao.pop();
142                         char fuh2=fuh1.charAt(0);//将String类型转为char类型
143                         String []rs1=new String[2];
144                         rs1=yunsuan2(st2, st1, fuh1);
145                         if(rs1[1].equals("error"))//如果运算中有问题,就结束运算
146                         {
147                             return "error";
148                         }
149                         else
150                         {
151                             num.push(rs1[0]+"");//将两数结果压入栈中
152                         }
153                     }
154                     else//优先级比较低,把运算符压入栈中
155                     {
156                         fuhao.push(ch+"");
157                         if((i+1)<a.length())
158                         {
159                             ch=a.charAt(++i);
160                         }
161                     }
162                 }
163             }
164             String rs=num.pop();
165             int wz=rs.indexOf("/");
166             if(wz!=-1)
167             {
168                 String fb=rs.substring(0, wz);
169                 String sb=rs.substring(wz+1,rs.length());
170                 int fb1=Integer.parseInt(fb);
171                 int sb1=Integer.parseInt(sb);
172                 if(fb1>=sb1&&fb1%sb1==0)
173                 {
174                     
175                     rs=(fb1/sb1)+"";
176                 }
177                 else if(fb1<sb1&&fb1%sb1!=0)
178                 {
179                     int commondivisor=maxyue(fb1, sb1);
180                     fb1/=commondivisor;
181                     sb1/=commondivisor;
182                     rs=fb1+"/"+sb1;
183                 }
184                 else
185                 {
186                     int commondivisor=maxyue(fb1, sb1);
187                     fb1/=commondivisor;
188                     sb1/=commondivisor;
189                     rs=(fb1/sb1)+"'"+(fb1%sb1)+"/"+sb1;
190                 }
191             }
192             return rs;
193         }
194         private static String[] tys(String fn,String sn,char c)//两个整数的运算
195         {
196             int a=Integer.parseInt(fn);
197             int b=Integer.parseInt(sn);
198             String []a1=new String [2];//a1[0]用来记录两数运算结果,a1[1]用来记录两数能否继续算下去
199             a1[0]=a1[1]="";
200             int d=0;//d用来短暂存取两数运算结果
201             int z=0;//除法中判断a1[0]是否需要加上d
202             if(c=='+')
203             {
204                 d=a+b;
205             }
206             else if(c=='-')
207             {
208                 if(a<b)
209                 {
210                     a1[1]="error";
211                     return a1;
212                 }
213                 else
214                 {
215                     d=a-b;
216                 }
217             }
218             else if(c=='*')
219             {
220                 d=a*b;
221             }
222             
223             else
224             {
225                 if(a%b==0&&a>=b)
226                 {
227                     d=a/b;
228                 }
229                 else
230                 {
231                     z=1;
232                     a1[0]=a+"/"+b;
233                 }
234             }
235             if(z==0)
236             {
237                 a1[0] = d+"";
238             }
239             return a1;
240         }
241         static String[] yunsuan2(String fn,String sn,String e)//两个数运算,分数,整数均可
242         {
243             String rs[]=new String[2];
244             rs[0]=rs[1]="";
245             int location1=fn.indexOf("/");
246             int location2=sn.indexOf("/");
247             if(location1==-1&&location2==-1)//两个整数的运算
248             {
249                 rs=tys(fn, sn, e.charAt(0));
250             }
251             else{
252                 int a=0;
253                 int b=0;
254                 int c=0;
255                 int d=0;
256                 if(location1!=-1&&location2!=-1)//两个数都为真分数
257                 {
258                     String r1=fn.substring(0,location1);
259                     String r2=fn.substring(location1+1,fn.length());
260                     String r3=sn.substring(0,location2);
261                     String r4=sn.substring(location2+1,sn.length());
262                     a=Integer.parseInt(r1);
263                     b=Integer.parseInt(r2);
264                     c=Integer.parseInt(r3);
265                     d=Integer.parseInt(r4);
266                 }
267                 else
268                 {
269                     if(location1==-1)
270                     {
271                         a=Integer.parseInt(fn);
272                         b=1;
273                         String r3=sn.substring(0,location2);
274                         String r4=sn.substring(location2+1,sn.length());
275                         c=Integer.parseInt(r3);
276                         d=Integer.parseInt(r4);
277                     }
278                     else
279                     {
280                         c=Integer.parseInt(sn);
281                         d=1;
282                         String r1=fn.substring(0,location1);
283                         String r2=fn.substring(location1+1,fn.length());
284                         a=Integer.parseInt(r1);
285                         b=Integer.parseInt(r2);
286                     }
287                 }
288                 int f=0,g=0,h=0,t=0;
289                 if(e.equals("+"))
290                 {
291                     if(b==d)
292                     {
293                         f=a+c;
294                         g=b;
295                     }
296                     else
297                     {
298                         g=b*d/maxyue(b,d);
299                         a=g/b*a;
300                         c=g/d*c;
301                         f=a+c;
302                     }
303                 }
304                 else if(e.equals("-"))
305                 {
306                     if(b==d)
307                     {
308                         f=a-c;
309                         if(f<=0)
310                         {
311                             rs[1]="error";
312                             return rs;
313                         }
314                         g=b;
315                     }
316                     else
317                     {
318                         g=b*d/maxyue(b,d);
319                         a=g/b*a;
320                         c=g/d*c;
321                         f=a-c;
322                         if(f<0)
323                         {
324                             rs[1]="error";
325                             return rs;
326                         }
327                     }
328                 }
329                 else if(e.equals("*"))
330                 {
331                     f=a*c;
332                     g=b*d;
333                 }
334                 else
335                 {
336                     f=a*d;
337                     g=b*c;
338                 }
339                 rs[0]=f+"/"+g;
340                 
341             }
342             
343             return rs;
344         }
345         static int[] operationAndStatistical(int ch1,int ch2,int num,int scope) throws ClassNotFoundException, SQLException//对结果进行操作并统计题数
346         {
347             int a[]=new int[2];
348             a[0]=0;
349             a[1]=0;//用来存取正确和错误的题数
350             String rs="";
351             int i=0;
352             String saveexcep[]=new String[num];
353             String saveresult[]=new String[num];
354             while(i<num)
355             {
356                 int n=(int) (Math.random()*3+2);
357                 if(ch1==1&&ch2==1)
358                 {
359                     rs=properFractionExithk(n,scope);
360                 }
361                 else if(ch1==1&&ch2==2)
362                 {
363                     rs=properFractionExit(n,scope);
364                 }
365                 else if(ch1==2&&ch2==1)
366                 {
367                     rs=generateExpressionkh(n,scope);
368                 }
369                 else 
370                 {
371                     rs=generationexception(n,scope);
372                 }
373                 String judgers=jisuanbh(rs);
374                 
375                 if(judgers.equals("error"))
376                 {
377                     System.out.print("");
378                 }
379                 else
380                 {
381                     saveexcep[i]=rs;
382                     saveresult[i]=judgers;
383                     System.out.println("请作答第"+(i+1)+"道题:"+rs+"=");
384 //                    System.out.print("你的答案是:");
385 //                    String rs1=sc.next();
386 //                    if(rs1.equals(judgers))
387 //                    {
388 //                        System.out.println("恭喜你,回答正确!");
389 //                        a[0]+=1;
390 //                    }
391 //                    else
392 //                    {
393 //                        System.out.println("回答错误!正确答案是:"+judgers);
394 //                        a[1]+=1;
395 //                    }
396                     i++;
397                 }
398                 }
399             System.out.println("你一共作答"+num+"道题,其中回答正确的题数是:"+a[0]+",回答错误的题数是:"+a[1]);
400             connect(saveexcep,saveresult,num);
401             return a;
402         }
403         static void connect(String []r1,String[]r2,int num) throws ClassNotFoundException, SQLException
404         {
405             Connection conn=null;
406             PreparedStatement pstmt = null;
407             ResultSet rs = null;//定义好链接,执行,和结果
408             String driverName = "com.mysql.jdbc.Driver";
409             String userName = "root";//数据库的名
410             String userPwd = "199762";//数据库密码
411             String dbName = "wzw1";//数据库的名字
412             String url1 = "jdbc:mysql://localhost:3306/" + dbName;
413             String url2 = "?user=" + userName + "&password=" + userPwd;
414             String url3 = "&useUnicode=true&characterEncoding=utf-8";//规定好字符集
415             String url = url1 + url2 + url3;
416             Class.forName(driverName);//连接数据库
417             conn = DriverManager.getConnection(url);
418             String sql="insert into wzw3(titleexception,result) value(?,?)";
419             pstmt=conn.prepareStatement(sql);
420             
421             for(int i=0;i<num;i++)
422             {
423                 pstmt.setString(1, r1[i]);
424                 pstmt.setString(2,r2[i]);
425                 pstmt.executeUpdate();
426             }
427             if(pstmt != null) {
428                 pstmt.close();
429             }
430             
431             if(conn != null) {
432                 System.out.println("导入成功");
433                 conn.close();
434             }
435         }    
436         
437         
438         //真分数运算
439         static String properFractionExit(int num,int scope)//产生不含括号含有真分数的表达式
440         {
441             int []r1=new int[2*num];//接受产生的数值
442             int []r2=new int[num-1];//接受符号
443             String[]r3={"+","-","*","/"};
444             
445             String rs="";//接受含括号的和不含括号的表达式
446             char ch='z';
447             while(ch=='z')
448             {
449                 int i=0;
450                 for(;i<2*num;i++)
451                 {
452                     r1[i]=(int) (Math.random()*(scope-1)+1);
453                 }
454                 ch='y';
455                 int j=0;
456                 while(j<2*num)
457                 {
458                     if(r1[j]>=r1[j+1])
459                     {
460                         ch='z';
461                         break;
462                     }
463                     j++;
464                     j++;
465                 }
466             }
467             for(int i=0;i<num-1;i++)
468             {
469                 r2[i]=(int) (Math.random()*4);
470             }
471             int j=0;
472             while(j<2*num-2)
473             {
474                 
475                 int commondivisor=maxyue(r1[j], r1[j+1]);
476                 r1[j]/=commondivisor;
477                 r1[j+1]/=commondivisor;
478                 if(r1[j]==r1[j+1])
479                 {
480                     rs+=" "+1+" ";
481                 }
482                 else
483                 {
484                     rs+=" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
485                 }
486                 j++;
487                 j++;
488             }
489             int commondivisor1=maxyue(r1[2*num-2],r1[2*num-1]);
490             r1[2*num-2]/=commondivisor1;
491             r1[2*num-1]/=commondivisor1;
492             if(r1[2*num-2]==r1[2*num-1])
493             {
494                 rs+=" "+1+" ";
495             }
496             else
497             {
498                 rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
499             }
500             return rs;
501         }
502         static String properFractionExithk(int num,int scope)//产生含括号含有真分数的表达式
503         {
504             int []r1=new int[2*num];//接受产生的数值
505             int []r2=new int[num-1];//接受符号
506             String[]r3={"+","-","*","/"};
507             int []r4=new int[num];//接受随机产生的括号
508             String []r5=new String[2*num];//将接受的括号的个数,转成字符串
509             String rs="";//接受含括号的和不含括号的表达式
510             r4=chansheng(num);
511             char ch='z';
512             while(ch=='z')
513             {
514                 
515                 for(int i=0;i<2*num;i++)
516                 {
517                     r1[i]=(int) (Math.random()*(scope-1)+1);
518                 }
519                 ch='y';
520                 int j=0;
521                 while(j<2*num)
522                 {
523                     if(r1[j]>=r1[j+1])
524                     {
525                         ch='z';
526                         break;
527                     }
528                     j++;
529                     j++;
530                 }
531             }
532             for(int i=0;i<num-1;i++)
533             {
534                 r2[i]=(int) (Math.random()*4);
535             }
536             
537             for(int i=0;i<2*num;i++)//产生括号的数组
538             {
539                 r5[i]="";
540                 if(i%2==0)
541                 {
542                     if(r4[i/2]>0)
543                     {
544                         for(int j=0;j<r4[i/2];j++)
545                         {
546                             r5[i]+="(";
547                         }
548                     }
549                     else if(r4[i/2]<0)
550                     {
551                         for(int j=0;j<0-r4[i/2];j++)
552                         {
553                             r5[i]+=")";
554                         }
555                     }
556                 }
557             }
558             int j=0;
559             while(j<2*num-2)
560             {
561                 int commondivisor=maxyue(r1[j], r1[j+1]);
562                 r1[j]/=commondivisor;
563                 r1[j+1]/=commondivisor;
564                     if(r5[j].equals(""))
565                     {
566                         rs+=" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
567                     }
568                     else if(r5[j].substring(0, 1).equals("("))
569                     {
570                         rs+=r5[j]+" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]];
571                     }
572                     else
573                     {
574                         rs+=" "+r1[j]+"/"+r1[j+1]+" "+r5[j]+r3[r2[(j+1)/2]];
575                     }
576                 
577                 j++;
578                 j++;
579             }
580             //算最后一个数
581             int commondivisor1=maxyue(r1[2*num-2],r1[2*num-1]);
582             r1[2*num-2]/=commondivisor1;
583             r1[2*num-1]/=commondivisor1;
584             
585                 if(r5[2*num-2].equals(""))
586                 {
587                     rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
588                 }
589                 else if(r5[2*num-2].substring(0, 1).equals("("))
590                 {
591                     rs+=r5[2*num-2]+" "+r1[2*num-2]+"/"+r1[2*num-1]+" ";
592                 }
593                 else
594                 {
595                     rs+=" "+r1[2*num-2]+"/"+r1[2*num-1]+" "+r5[2*num-2];
596                 }
597             return rs;
598         }
599         
600         
601         
602         //整数运算
603         private static String generateExpressionkh(int num,int scope)//产生带括号的整数表达式
604         {
605             int a1[]=new int[num];
606             int a2[]=new int[num-1];
607             int a3[]=new int[num];
608             String[]a5=new String[num];
609             String[] a4={"+","-","*","/"};
610             for(int i=0;i<num;i++)
611             {
612                 a1[i]=(int) (Math.random()*(scope-1)+1);
613             }
614             for(int i=0;i<num-1;i++)
615             {
616                 a2[i]=(int) (Math.random()*4);
617             }
618             a3=chansheng(num);
619             for(int i=0;i<num;i++)
620             {
621                 a5[i]="";
622                 if(a3[i]<0)
623                 {
624                     int c=0-a3[i];
625                     for(int j=0;j<c;j++)
626                     {
627                         a5[i]+=")";
628                     }
629                 }
630                 else
631                 {
632                     for(int j=0;j<a3[i];j++)
633                     {
634                         a5[i]+="(";
635                     }
636                 }
637             }
638             String t="";
639             for(int i=0;i<num-1;i++)
640             {
641                 if(a3[i]>0)
642                 {
643                     t+=a5[i]+" "+a1[i]+" "+a4[a2[i]];
644                 }
645                 else
646                 {
647                     t+=" "+a1[i]+" "+a5[i]+a4[a2[i]];
648                 }
649             }
650             if(a3[num-1]>0)
651             {
652                 t+=a5[num-1]+" "+a1[num-1]+" ";
653             }
654             else
655             {
656                 t+=" "+a1[num-1]+" "+a5[num-1];
657             }
658             return t;
659         }
660         private static String generationexception(int num,int scope)//产生不带括号的表达式
661         {
662             int a1[]=new int[num];
663             int a2[]=new int[num-1];
664             int a3[]=new int[num];
665             String[] a4={"+","-","*","/"};
666             for(int i=0;i<num;i++)
667             {
668                 a1[i]=(int) (Math.random()*(scope-1)+1);
669             }
670             for(int i=0;i<num-1;i++)
671             {
672                 a2[i]=(int) (Math.random()*4);
673             }
674             String t="";
675             for(int i=0;i<num-1;i++)
676             {
677                 if(a3[i]>0)
678                 {
679                     t+=" "+a1[i]+" "+a4[a2[i]];
680                 }
681                 else
682                 {
683                     t+=" "+a1[i]+" "+a4[a2[i]];
684                 }
685             }
686             if(a3[num-1]>0)
687             {
688                 t+=" "+a1[num-1]+" ";
689             }
690             else
691             {
692                 t+=" "+a1[num-1]+" ";
693             }
694             return t;
695         }
696         
697 }
View Code

 结果截图:

上面这个截图显示了产生不定长度的表达式,如果结果是假分数,答案应该是第五道题的形式,能对所做的正误进行判断。

 测试产生的题目能否保存到数据库中

  实验总结:自己在设计算法方面还是有一定的缺陷,这些算法想着的时候感觉很简单,但是让我敲出来的时候就难的不行,而且我以后不会写到最后在测试写的代码正不正确,因为测试的时候太难了,不知道 去哪找

这次尝试着没写完一个小的方法,就测试一下,这样可以极大缩短错误的范围,用起来效果不错。我尝试着在新的方法和变量命名的时候查了一下,这样既练习了英语,也使代码更加规范,可能用的不太合适,英语比较差,

有时候会找不到用英文命名的方法和变量,相信以后会慢慢适应

原文地址:https://www.cnblogs.com/qingtianxt/p/6575832.html