软件工程概论作业三——四则运算三

一、题目要求 

 

二、源代码

  1 package size;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.util.Scanner;
  6 public class Timu3 {
  7 
  8     //保存整数结果
  9     static int re;
 10     //保存整数带余数的结果
 11     static String re1;
 12     //保存分式结果
 13     static String refenshu;
 14     //保存三个整数公式的结果
 15     static int resan;
 16     
 17     //****最大公约****
 18     static double getGcd(double m,double n){
 19             while(n > 0){
 20                 double temp = m % n;
 21                 m = n;
 22                 n = temp;
 23             }
 24             return m;
 25         }
 26     //****最大公约****
 27     
 28      //***最小公倍数***
 29     static double getLcm(double m,double n){
 30          double gcd = getGcd(m,n);
 31          double result = m*n / gcd;
 32             return result;
 33         }
 34      //***最小公倍数***
 35     
 36     //*****题目数量的函数*****
 37     static int num()
 38     {
 39         int num;
 40         Scanner in =  new Scanner(System.in);
 41         System.out.print("请输入题目的数量:");
 42         num = in.nextInt();
 43         return num;
 44     }
 45     //*****题目数量的函数*****
 46     
 47     //*****生成三个数的式子的函数******
 48     static String sgongshi(int r,int q)
 49     {
 50         String shi,shizi="";
 51         int a=(int)(Math.random()*(r)%(r-q+1)+q);
 52         int e=(int)(Math.random()*4);
 53         shi=function(r,q,12);
 54         if(e==0)
 55         {
 56             int f=(int)(Math.random()*4);
 57             if(f==1||f==0)
 58             {
 59                 shizi=a+" + "+"( "+shi+" )";
 60                 resan=a+re;
 61             }
 62             if(f==2||f==3)
 63             {
 64                 shizi="( "+shi+" )"+" + "+a;
 65                 resan=a+re;
 66             }
 67         }
 68         if(e==1)
 69         {
 70             int f=(int)(Math.random()*4);
 71             if(f==1||f==0)
 72             {
 73                 while(a-re<0)
 74                 {
 75                     shi=function(r,q,12);
 76                 }
 77                 shizi=a+" - "+"( "+shi+" )";
 78                 resan=a-re;
 79             }
 80             if(f==2||f==3)
 81             {
 82                 while(a-re>0)
 83                 {
 84                     shi=function(r,q,12);
 85                 }
 86                 shizi="( "+shi+" )"+" - "+a;
 87                 resan=re-a;
 88             }
 89         }
 90         if(e==2)
 91         {
 92             int f=(int)(Math.random()*4);
 93             if(f==1||f==0)
 94             {
 95                 shizi=a+" × "+"( "+shi+" )";
 96                 resan=a*re;
 97             }
 98             if(f==2||f==3)
 99             {
100                 shizi="( "+shi+" )"+" × "+a;
101                 resan=a*re;
102             }
103         }
104         if(e==3)
105         {
106             int f=(int)(Math.random()*4);
107             if(f==1||f==0)
108             {
109                 while(re==0)
110                 {
111                     shi=function(r,q,12);    
112                 }
113                 while(a%re!=0)
114                 {
115                     shi=function(r,q,12);
116                     while(re==0)
117                     {
118                         shi=function(r,q,12);
119                     }
120                 }
121                 shizi=a+" ÷ "+"( "+shi+" )";
122                 resan=a/re;
123             }
124             if(f==2||f==3)
125             {
126                 while(a==0)
127                 {
128                     a=(int)(Math.random()*(r)%(r-q+1)+q);
129                 }
130                 while(re%a!=0)
131                 {
132                     shi=function(r,q,12);
133                 }
134                 shizi="( "+shi+" )"+" ÷ "+a;
135                 resan=re/a;
136             }
137         }
138         return shizi;
139     }
140     //*****生成三个数的式子的函数******
141     
142     //*****************************生成分数式子的函数************************************************************
143     static String fun(int r ,int fun)
144     {
145         //1有乘除法2没有乘除法
146         String fu ="";
147         String shizi="";
148         int a=(int)(Math.random()*(r+1));
149         int b=(int)(Math.random()*(r+1));
150         int c=(int)(Math.random()*(r+1));
151         int d=(int)(Math.random()*(r+1));
152         int e=(int)(Math.random()*4);
153         
154         //1有乘除法****1
155         if(fun==1)
156         {
157         if(e==0)
158         {
159             fu="+";
160             while(a>=b||c>=d||b==0||d==0)
161             {
162                  a=(int)(Math.random()*(r+1));
163                  b=(int)(Math.random()*(r+1));
164                  c=(int)(Math.random()*(r+1));
165                  d=(int)(Math.random()*(r+1));
166             }
167             //约分
168             int aa=(int) getGcd(a,b);
169             int cc=(int) getGcd(c,d);
170             a=a/aa; b=b/aa;
171             c=c/cc; d=d/cc;
172             shizi=a+"/"+b+" "+fu+" "+c+"/"+d;
173             int gongbei=(int) getLcm(b,d);
174             a=a*(gongbei/b);
175             c=c*(gongbei/d);
176             a=a+c;
177             int t=(int) getGcd(a,gongbei);
178             a=a/t;
179             gongbei=gongbei/t;
180             if(a==gongbei)
181                 refenshu="1";
182             else if(a==0)
183                 refenshu="0";
184             else if(a>gongbei)
185             {
186                 int s=a%gongbei;
187                 refenshu=1+"’"+s+"/"+gongbei;
188             }
189             else
190             refenshu=a+"/"+gongbei;
191             
192         }
193         if(e==1)
194         {
195             fu="-";
196             double y=getLcm(b,d);
197             double a1=y/b;
198             double c1=y/d;
199             double a2=a*a1;
200             double c2=c*c1;
201             while(a>=b||c>=d||b==0||d==0||a2<c2)
202             {
203                  a=(int)(Math.random()*(r+1));
204                  b=(int)(Math.random()*(r+1));
205                  c=(int)(Math.random()*(r+1));
206                  d=(int)(Math.random()*(r+1));
207                   y=getLcm(b,d);
208                      a1=y/b;
209                      c1=y/d;
210                      a2=a*a1;
211                      c2=c*c1;
212             }
213             int aa=(int) getGcd(a,b);
214             int cc=(int) getGcd(c,d);
215             a=a/aa; b=b/aa;
216             c=c/cc; d=d/cc;
217             shizi=a+"/"+b+" "+fu+" "+c+"/"+d;
218             int gongbei=(int) getLcm(b,d);
219             a=a*(gongbei/b);
220             c=c*(gongbei/d);
221             a=a-c;
222             int t=(int) getGcd(a,gongbei);
223             a=a/t;
224             gongbei=gongbei/t;
225             if(a==gongbei)
226                 refenshu="1";
227             else if(a==0)
228                 refenshu="0";
229             else
230             refenshu=a+"/"+gongbei;
231         }
232         if(e==2)
233         {
234             fu="×";
235             while(a>=b||c>=d||b==0||d==0)
236             {
237                  a=(int)(Math.random()*(r+1));
238                  b=(int)(Math.random()*(r+1));
239                  c=(int)(Math.random()*(r+1));
240                  d=(int)(Math.random()*(r+1));
241             }
242             int aa=(int) getGcd(a,b);
243             int cc=(int) getGcd(c,d);
244             a=a/aa; b=b/aa;
245             c=c/cc; d=d/cc;
246             shizi=a+"/"+b+" "+fu+" "+c+"/"+d;
247             a=a*c;
248             b=b*d;
249             int flag1=(int) getGcd(a,b);
250             a=a/flag1;b=b/flag1;
251             if(a==0)
252                 refenshu="0";
253             else
254                 refenshu=a+"/"+b;
255         }
256         if(e==3)
257         {
258             fu="÷";
259             while(a>=b||c>=d||b==0||d==0||c==0)
260             {
261                  a=(int)(Math.random()*(r+1));
262                  b=(int)(Math.random()*(r+1));
263                  c=(int)(Math.random()*(r+1));
264                  d=(int)(Math.random()*(r+1));
265             }
266             int aa=(int) getGcd(a,b);
267             int cc=(int) getGcd(c,d);
268             a=a/aa; b=b/aa;
269             c=c/cc; d=d/cc;
270             shizi=a+"/"+b+" "+fu+" "+c+"/"+d;
271             a=a*d;
272             b=b*c;
273             int flag1=(int) getGcd(a,b);
274             a=a/flag1;b=b/flag1;
275             if(a==0)
276                 refenshu="0";
277             else if(a==b)
278                 refenshu="1";
279             else if(b==1)
280                 refenshu=a+"";
281             else if(a>b)
282             {
283                 int s=a%b;
284                 int s1=a/b;
285                 refenshu=s1+"’"+s+"/"+b;
286             }
287             else
288                 refenshu=a+"/"+b;
289         }
290         }
291         
292         //2没有有乘除法***2
293         if(fun==2)
294         {
295         if(e==0)
296         {
297             fu="+";
298             while(a>=b||c>=d||b==0||d==0)
299             {
300                  a=(int)(Math.random()*(r+1));
301                  b=(int)(Math.random()*(r+1));
302                  c=(int)(Math.random()*(r+1));
303                  d=(int)(Math.random()*(r+1));
304             }
305             int aa=(int) getGcd(a,b);
306             int cc=(int) getGcd(c,d);
307             a=a/aa; b=b/aa;
308             c=c/cc; d=d/cc;
309             shizi=a+"/"+b+" "+fu+" "+c+"/"+d;
310             int gongbei=(int) getLcm(b,d);
311             a=a*(gongbei/b);
312             c=c*(gongbei/d);
313             a=a+c;
314             int t=(int) getGcd(a,gongbei);
315             a=a/t;
316             gongbei=gongbei/t;
317             if(a==gongbei)
318                 refenshu="1";
319             else if(a==0)
320                 refenshu="0";
321             else if(a>gongbei)
322             {
323                 int s=a%gongbei;
324                 refenshu=1+"’"+s+"/"+gongbei;
325             }
326             else
327             refenshu=a+"/"+gongbei;
328         }
329         if(e==1)
330         {
331             fu="-";
332             double y=getLcm(b,d);
333             double a1=y/b;
334             double c1=y/d;
335             double a2=a*a1;
336             double c2=c*c1;
337             while(a>=b||c>=d||b==0||d==0||a2<c2)
338             {
339                  a=(int)(Math.random()*(r+1));
340                  b=(int)(Math.random()*(r+1));
341                  c=(int)(Math.random()*(r+1));
342                  d=(int)(Math.random()*(r+1));
343                   y=getLcm(b,d);
344                      a1=y/b;
345                      c1=y/d;
346                      a2=a*a1;
347                      c2=c*c1;
348             }
349             int aa=(int) getGcd(a,b);
350             int cc=(int) getGcd(c,d);
351             a=a/aa; b=b/aa;
352             c=c/cc; d=d/cc;
353             shizi=a+"/"+b+" "+fu+" "+c+"/"+d;
354             int gongbei=(int) getLcm(b,d);
355             a=a*(gongbei/b);
356             c=c*(gongbei/d);
357             a=a-c;
358             int t=(int) getGcd(a,gongbei);
359             a=a/t;
360             gongbei=gongbei/t;
361             if(a==gongbei)
362                 refenshu="1";
363             else if(a==0)
364                 refenshu="0";
365             else
366             refenshu=a+"/"+gongbei;
367         }
368         if(e==2)
369         {
370             fu="+";
371             while(a>=b||c>=d||b==0||d==0)
372             {
373                  a=(int)(Math.random()*(r+1));
374                  b=(int)(Math.random()*(r+1));
375                  c=(int)(Math.random()*(r+1));
376                  d=(int)(Math.random()*(r+1));
377             }
378             int aa=(int) getGcd(a,b);
379             int cc=(int) getGcd(c,d);
380             a=a/aa; b=b/aa;
381             c=c/cc; d=d/cc;
382             shizi=a+"/"+b+" "+fu+" "+c+"/"+d;
383             int gongbei=(int) getLcm(b,d);
384             a=a*(gongbei/b);
385             c=c*(gongbei/d);
386             a=a+c;
387             int t=(int) getGcd(a,gongbei);
388             a=a/t;
389             gongbei=gongbei/t;
390             if(a==gongbei)
391                 refenshu="1";
392             else if(a==0)
393                 refenshu="0";
394             else if(a>gongbei)
395             {
396                 int s=a%gongbei;
397                 refenshu=1+"’"+s+"/"+gongbei;
398             }
399             else
400             refenshu=a+"/"+gongbei;
401         }
402         if(e==3)
403         {
404             fu="-";
405             double y=getLcm(b,d);
406             double a1=y/b;
407             double c1=y/d;
408             double a2=a*a1;
409             double c2=c*c1;
410             while(a>=b||c>=d||b==0||d==0||a2<c2)
411             {
412                  a=(int)(Math.random()*(r+1));
413                  b=(int)(Math.random()*(r+1));
414                  c=(int)(Math.random()*(r+1));
415                  d=(int)(Math.random()*(r+1));
416                   y=getLcm(b,d);
417                      a1=y/b;
418                      c1=y/d;
419                      a2=a*a1;
420                      c2=c*c1;
421             }
422             int aa=(int) getGcd(a,b);
423             int cc=(int) getGcd(c,d);
424             a=a/aa; b=b/aa;
425             c=c/cc; d=d/cc;
426             shizi=a+"/"+b+" "+fu+" "+c+"/"+d;
427             int gongbei=(int) getLcm(b,d);
428             a=a*(gongbei/b);
429             c=c*(gongbei/d);
430             a=a-c;
431             int t=(int) getGcd(a,gongbei);
432             a=a/t;
433             gongbei=gongbei/t;
434             if(a==gongbei)
435                 refenshu="1";
436             else if(a==0)
437                 refenshu="0";
438             else
439             refenshu=a+"/"+gongbei;
440         }
441         }
442         return shizi;
443     }
444     //************生成分数式子的函数*************************
445     
446     //***************生成整数公式的函数**********************
447     static String function(int r ,int q, int fun)
448     {
449         //1有乘除法 2加减无负数 3除法无余数(12,1,0)
450         String fu ="";
451         String shizi="";
452         int a=(int)(Math.random()*(r)%(r-q+1)+q);
453         int b=(int)(Math.random()*(r)%(r-q+1)+q);
454         int c=(int)(Math.random()*4);
455         
456         //1乘除法 2除法无余数****12
457         if(fun==12)
458         {
459             if(c==0)
460             {
461                 fu="+";
462                 shizi=a+" "+fu+" "+b;
463                 re=a+b;
464                 re1=String.valueOf(re);
465             }
466             if(c==1)
467             {
468                 fu="-";
469                 //排除负数可能性
470                 while(a-b<0)
471                 {
472                     a=(int)(Math.random()*(r)%(r-q+1)+q);
473                     b=(int)(Math.random()*(r)%(r-q+1)+q);
474                 }
475                 shizi=a+" "+fu+" "+b;
476                 re=a-b;
477                 re1=String.valueOf(re);
478             }
479             if(c==2)
480             {
481                 fu="×";
482                 shizi=a+" "+fu+" "+b;
483                 re=a*b;
484                 re1=String.valueOf(re);
485             }
486             if(c==3)
487             {
488                 fu="÷";
489                 //排除分母为0的可能性和余数的可能性
490                 while(b==0||a%b!=0)
491                 {
492                     a=(int)(Math.random()*(r)%(r-q+1)+q);
493                     b=(int)(Math.random()*(r)%(r-q+1)+q);
494                 }    
495                 re=a/b;
496                 shizi=a+" "+fu+" "+b;
497                 re1=String.valueOf(re);
498             }
499         }
500         
501         
502         //1乘除法 2除法无余数****1
503         if(fun==1)
504         {
505             if(c==0)
506             {
507                 fu="+";
508                 re=a+b;
509                 re1=String.valueOf(re);
510                 shizi=a+" "+fu+" "+b;
511             }
512             if(c==1)
513             {
514                 fu="-";
515                 //排除负数可能性
516                 while(a-b<0)
517                 {
518                     a=(int)(Math.random()*(r)%(r-q+1)+q);
519                     b=(int)(Math.random()*(r)%(r-q+1)+q);
520                 }
521                 shizi=a+" "+fu+" "+b;
522                 re=a-b;
523                 re1=String.valueOf(re);
524             }
525             if(c==2)
526             {
527                 fu="×";
528                 re=a*b;
529                 re1=String.valueOf(re);
530                 shizi=a+" "+fu+" "+b;
531             }
532             if(c==3)
533             {
534                 fu="÷";
535                 //排除分母为0的可能性
536                 while(b==0)
537                 {
538                     b=(int)(Math.random()*(r)%(r-q+1)+q);
539                 }    
540                 re=a/b;
541                 if(a%b!=0)
542                 {
543                     int flag1=a%b;
544                     re1=String.valueOf(re)+"..."+String.valueOf(flag1);
545                 }
546                 else
547                     re1=String.valueOf(re);
548                 shizi=a+" "+fu+" "+b;
549             }
550         }
551         
552         //1乘除法 2除法无余数****0
553         if(fun==0)
554         {
555             if(c==0)
556             {
557                 fu="+";
558                 re=a+b;
559                 re1=String.valueOf(re);
560                 shizi=a+" "+fu+" "+b;
561             }
562             if(c==1)
563             {
564                 fu="-";
565                 //排除负数可能性
566                 while(a-b<0)
567                 {
568                     a=(int)(Math.random()*(r)%(r-q+1)+q);
569                     b=(int)(Math.random()*(r)%(r-q+1)+q);
570                 }
571                 re=a-b;
572                 re1=String.valueOf(re);
573                 shizi=a+" "+fu+" "+b;
574             }
575             if(c==2)
576             {
577                 fu="+";
578                 re=a+b;
579                 re1=String.valueOf(re);
580                 shizi=a+" "+fu+" "+b;
581             }
582             if(c==3)
583             {
584                 fu="-";
585                 //排除负数可能性
586                 while(a-b<0)
587                 {
588                     a=(int)(Math.random()*(r)%(r-q+1)+q);
589                     b=(int)(Math.random()*(r)%(r-q+1)+q);
590                 }
591                 re=a-b;
592                 re1=String.valueOf(re);
593                 shizi=a+" "+fu+" "+b;
594             }
595         }
596             return shizi;
597     }
598     //***************生成整数公式的函数**********************
599     
600     //***下面是主函数*****
601     public static void main(String[] args) {
602         // TODO 自动生成的方法存根
603         //连接数据库****************
604         try {
605             Connection con = null; //定义一个MYSQL链接对象
606             Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动
607             con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); //链接本地MYSQL
608             java.sql.Statement stmt; //创建声明
609             stmt = con.createStatement();
610             int num1,r = 0,q = 0,r1,m=0,fun ;
611             int zhengque = 0;//计算正确的个数(分数)
612             int cuowu = 0;//计算错误的个数(分数)
613             int zhengque1 = 0;//计算正确的个数(整数)
614             int cuowu1 = 0;//计算错误的个数(整数)
615             int zhengque5 = 0;//计算正确的个数(三个整数带括号)
616             int cuowu5 = 0;//计算错误的个数(三个整数带括号)
617             while(m==0)
618             {
619                 int flag=0;
620                 System.out.print("1:真分数 2:整数 3:三个整数 4:退出 请输入:");
621                 Scanner in2 =  new Scanner(System.in);
622                 r1 = in2.nextInt();
623                 if(r1==1)//******真分数****
624                 {
625                     String correct="",wrong="";
626                     num1=num();
627                     int zhengque2[]=new int[num1+10];
628                     int cuowu2[]=new int[num1+10];
629                     String Array1[]=new String[num1];
630                     String Arr1[]=new String[num1];
631                     System.out.print("请输入算数题中出现的数的最大值:");
632                     Scanner in1 =  new Scanner(System.in);
633                     r = in1.nextInt();
634             
635                     System.out.println("请输入算数题的控制项(1有乘除法 2没有乘除法)");
636                     System.out.println("例如:1是有乘除法");
637                     System.out.print("请输入控制项:");
638                     Scanner in21 =  new Scanner(System.in);
639                     fun = in21.nextInt();
640                     while(flag==0)
641                     {
642                         if(fun==1||fun==2)
643                         {
644                             for(int i=0;i<num1;i++)
645                             {
646                                 Array1[i]=fun(r,fun);
647                                 //查重
648                                 for(int z=0;z<i;z++)
649                                 {
650                                     if((Array1[z].equals(Array1[i])))
651                                     {
652                                         Array1[i]=fun(r,fun);
653                                         z=0;
654                                     }
655                                 }    
656                                 flag=1;
657                                 System.out.print(i+1+": "+Array1[i]+" = ");
658                                 //将题目和答案写入数据库表
659                                 String sql="INSERT INTO 四则运算题目1 (shizi,result) VALUES ('"+Array1[i]+" = "+"','"+refenshu+"')";
660                                 stmt.executeUpdate(sql);
661                                 //System.out.print("请输入结果:");
662                                 String result;
663                                 Scanner in2223 =  new Scanner(System.in);
664                                 result=in2223.next();
665                                 if(result.equals(refenshu))
666                                 {
667                                     System.out.println("恭喜你,回答正确!!!");
668                                     zhengque2[zhengque]=i;
669                                     zhengque++;
670                                 }
671                                 else
672                                 {
673                                     System.out.println("回答错误!正确答案为:"+refenshu);
674                                     cuowu2[cuowu]=i;
675                                     cuowu++;
676                                 }
677                             }
678                         }
679                         else
680                         {
681                             System.out.println("输入错误,请重新输入!");
682                             flag=1;
683                         }
684                     }        
685                     //输出正确题号和错误题号
686                     System.out.println();
687                     System.out.println("恭喜你,回答正确"+zhengque+"道题!!!");
688                     if(zhengque>0)
689                     {
690                         System.out.print("正确题号:");
691                         for(int i=0;i<zhengque;i++)
692                         {
693                             System.out.print(zhengque2[i]+1+" ");
694                             int corr=0;
695                             corr=zhengque2[i]+1;
696                             correct=correct+corr+"  ";
697                         }
698                     }
699                     System.out.println();
700                     if(cuowu>0)
701                     {
702                         System.out.print("错误题号:");
703                         for(int i=0;i<cuowu;i++)
704                         {
705                             System.out.print(cuowu2[i]+1+" ");
706                             int wro=0;
707                             wro=cuowu2[i]+1;
708                             wrong=wrong+wro+"  ";
709                         }
710                     }
711                     //将正确错误的题号写入数据库表
712                     String sql="INSERT INTO Grade (Correct,Wrong) VALUES ('"+zhengque+"("+correct+")','"+cuowu+"("+wrong+")')";
713                     stmt.executeUpdate(sql);
714                     correct="";
715                     wrong="";
716                     zhengque=0;
717                     cuowu=0;
718                 }
719                 if(r1==2)//****整数****
720                 {
721                     String correct="",wrong="";
722                     num1=num();
723                     int zhengque4[]=new int[num1+10];
724                     int cuowu4[]=new int[num1+10];
725                     String Array[]=new String[num1];
726                     String Arr[]=new String[num1];
727                     //进行最大值最小值的顺序判断***
728                     int w1=0;;
729                     while(w1==0)
730                     {
731                         System.out.print("请输入算数题中出现的数的最大值和最小值:");
732                         Scanner in1 =  new Scanner(System.in);
733                         r = in1.nextInt();
734                         q = in1.nextInt();
735                         if(r>=q)
736                             w1=1;
737                         else 
738                         {
739                             w1=0;
740                             System.out.println("输入错误!");
741                         }
742                     }
743         
744                     System.out.println("请输入算数题的控制项(1有乘除法2除法无余数 0都未选择)");
745                     System.out.println("例如:12是有乘除法,除法无余数");
746                     System.out.print("请输入控制项:");
747                     Scanner in211 =  new Scanner(System.in);
748                     fun = in211.nextInt();
749                     while(flag==0)
750                     {
751                         if(fun==12||fun==1||fun==0)
752                         {
753                             for(int i=0;i<num1;i++)
754                             {
755                                 Array[i]=function(r,q,fun);
756                                 //查重
757                                 for(int z=0;z<i;z++)
758                                 {
759                                     if((Array[z].equals(Array[i])))
760                                     {
761                                         Array[i]=function(r,q,fun);
762                                         z=0;
763                                     }
764                                 }
765                                 flag=1;
766                                 System.out.print(i+1+": "+Array[i]+" = ");
767                                 //将题目和答案写入数据库表
768                                 String sql="INSERT INTO 四则运算题目2 (shizi,result) VALUES ('"+Array[i]+" = "+"','"+re1+"')";
769                                 stmt.executeUpdate(sql);
770                                 //System.out.print("请输入结果:");
771                                 String result;
772                                 Scanner in222 =  new Scanner(System.in);
773                                 result=in222.next();
774                                 if(result.equals(re1))
775                                 {
776                                     System.out.println("恭喜你,回答正确!!!");
777                                     zhengque4[zhengque1]=i;
778                                     zhengque1++;
779                                 }
780                                 else
781                                 {
782                                     System.out.println("回答错误!正确答案为:"+re1);
783                                     cuowu4[cuowu1]=i;
784                                     cuowu1++;
785                                 }
786                             }
787                         }
788                         if(fun!=12&&fun!=1&&fun!=0)
789                         {
790                             System.out.println("输入错误,请重新输入!");
791                             flag=1;
792                         }
793                     }
794                     System.out.println();
795                     //输出正确题号和错误题号
796                     System.out.println("恭喜你,回答正确"+zhengque1+"道题!!!");
797                     if(zhengque1>0)
798                     {
799                         System.out.print("正确题号:");
800                         for(int i=0;i<zhengque1;i++)
801                         {
802                             System.out.print(zhengque4[i]+1+" ");
803                             int corr=0;
804                             corr=zhengque4[i]+1;
805                             correct=correct+corr+"  ";
806                         }
807                     }
808                     System.out.println();
809                     if(cuowu1>0)
810                     {
811                         System.out.print("错误题号:");
812                         for(int i=0;i<cuowu1;i++)
813                         {
814                             System.out.print(cuowu4[i]+1+" ");
815                             int wro=0;
816                             wro=cuowu4[i]+1;
817                             wrong=wrong+wro+"  ";
818                         }
819                     }
820                     //将正确错误的题号写入数据库表
821                     String sql="INSERT INTO Grade (Correct,Wrong) VALUES ('"+zhengque1+"("+correct+")','"+cuowu1+"("+wrong+")')";
822                     stmt.executeUpdate(sql);
823                     correct="";
824                     wrong="";
825                     zhengque1=0;
826                     cuowu1=0;
827                 }
828                 if(r1==3)//括号的式子
829                 {
830                     String correct="",wrong="";
831                     num1=num();
832                     int zhengque6[]=new int[num1+10];
833                     int cuowu6[]=new int[num1+10];
834                     String Ar[]=new String[num1];
835                     //进行最大值最小值的顺序判断***
836                     int w1=0;;
837                     while(w1==0)
838                     {
839                         System.out.print("请输入算数题中出现的数的最大值和最小值:");
840                         Scanner in1 =  new Scanner(System.in);
841                         r = in1.nextInt();
842                         q = in1.nextInt();
843                         if(r>=q)
844                             w1=1;
845                         else 
846                         {
847                             w1=0;
848                             System.out.println("输入错误!");
849                         }
850                     }
851                     for(int i=0;i<num1;i++)
852                     {
853                         Ar[i]=sgongshi(r,q);
854                         //查重
855                         for(int z=0;z<i;z++)
856                         {
857                             if((Ar[z].equals(Ar[i])))
858                             {
859                                 Ar[i]=sgongshi(r,q);
860                                 z=0;
861                             }
862                         }
863                         System.out.print(i+1+": "+Ar[i]+" = ");
864                         //将题目和答案写入数据库表
865                         String sql="INSERT INTO 四则运算题目3 (shizi,result) VALUES ('"+Ar[i]+" = "+"','"+resan+"')";
866                         stmt.executeUpdate(sql);
867                         //System.out.print("请输入结果:");
868                         String result;
869                         Scanner in222 =  new Scanner(System.in);
870                         result=in222.next();
871                         if(result.equals(String.valueOf(resan)))
872                         {
873                             System.out.println("恭喜你,回答正确!!!");
874                             zhengque6[zhengque5]=i;
875                             zhengque5++;
876                         }
877                         else
878                         {
879                             System.out.println("回答错误!正确答案为:"+resan);
880                             cuowu6[cuowu5]=i;
881                             cuowu5++;
882                         }
883                     }
884                     System.out.println();
885                     //输出正确题号和错误题号
886                     System.out.println("恭喜你,回答正确"+zhengque5+"道题!!!");
887                     if(zhengque5>0)
888                     {
889                         System.out.print("正确题号:");
890                         for(int i=0;i<zhengque5;i++)
891                         {
892                             System.out.print(zhengque6[i]+1+" ");
893                             int corr=0;
894                             corr=zhengque6[i]+1;
895                             correct=correct+corr+"  ";
896                         }
897                     }
898                     System.out.println();
899                     if(cuowu5>0)
900                     {
901                         System.out.print("错误题号:");
902                         for(int i=0;i<cuowu5;i++)
903                         {
904                             System.out.print(cuowu6[i]+1+" ");
905                             int wro=0;
906                             wro=cuowu6[i]+1;
907                             wrong=wrong+wro+"  ";
908                         }
909                     }
910                     //将正确错误的题号写入数据库表
911                     String sql="INSERT INTO Grade (Correct,Wrong) VALUES ('"+zhengque5+"("+correct+")','"+cuowu5+"("+wrong+")')";
912                     stmt.executeUpdate(sql);
913                     correct="";
914                     wrong="";
915                     zhengque5=0;
916                     cuowu5=0;
917                 }
918                 if(r1==4)//*****退出******
919                 {
920                     m=1;
921                     System.out.print("谢谢使用!");
922                 }
923                 System.out.println();
924             }
925             } 
926             catch (Exception e) {
927                 System.out.print("MYSQL ERROR:" + e.getMessage());
928             }    
929     }
930 }

三、实验结果截图

四、总结

  1、部分方法未能实现,参考过度,解决方法不完善!

  2、了解SQL语句及一般方法。

原文地址:https://www.cnblogs.com/XiaoPiHaiEr/p/6575322.html