四则运算终极版

  1 package szys;
  2 import java.util.Scanner;
  3 import java.util.Random;
  4 public class szys2 
  5 {
  6 
  7     public static void main(String[] args) 
  8     {
  9         // TODO 自动生成的方法存根
 10         System.out.println("********四则运算*********");
 11         System.out.println("******1.加减运算*********");
 12         System.out.println("******2.加减乘除运算******");
 13         System.out.println("**********************");
 14         Scanner n=new Scanner(System.in);
 15         System.out.print("请输入运算选项:  ");
 16         int n1=n.nextInt();
 17         switch(n1)
 18         {
 19             case 1:add();
 20                 break;
 21             case 2:mul();
 22                 break;
 23         }
 24     }
 25     
 26     public static void add()
 27     {              //加减法
 28         System.out.println("******加法运算******");
 29         int i,count1=0,count2=0;
 30         char[] ch={'+','-',};//字符数组
 31         int a[][]=new int[10000][3];
 32         int b[][]=new int[10000][4];
 33         Random r=new Random();
 34         Scanner s=new Scanner(System.in);
 35         int n1 = 0;
 36         System.out.print("请输入算术题的个数:  ");
 37         int m1=s.nextInt();
 38         //System.out.print("请输入每行打印的个数:  ");
 39         //int m2=s.nextInt();
 40         System.out.print("加减是否有负数(1/0):");
 41         int c1=s.nextInt();
 42         if(c1==1)
 43         {
 44             System.out.println("请选择数值范围:");
 45             int a1=s.nextInt();
 46             int a2=s.nextInt();
 47             int count=0;
 48             System.out.println("******算数如下******");
 49             while(count<m1)
 50             {
 51                 boolean m=true;
 52                 int s1=(r.nextInt(a2-a1+1)+a1);
 53                 int s2=(r.nextInt(a2-a1+1)+a1);
 54                 int s3=(r.nextInt(ch.length));
 55                 for(i=0;i<=count;i++)
 56                 {
 57                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
 58                     {
 59                         m=false;
 60                     }
 61                 }
 62                 if(m=true)
 63                 {
 64                     a[count][0]=s1;
 65                     a[count][1]=s2;
 66                     a[count][2]=s3;
 67                 }
 68                 else
 69                 {
 70                     count--;
 71                 }
 72                 count++;
 73             }
 74            for(i=0;i<m1;i++)
 75              {
 76                  switch(a[i][2])
 77                  {
 78                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
 79                  break;}
 80                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
 81                  break;}
 82                  }
 83                  Scanner s1=new Scanner(System.in);
 84                  n1=s.nextInt();
 85                  if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1])
 86                  {
 87                      count1++;
 88                  }
 89                  else
 90                  {
 91                      b[count2][0]=a[i][0];
 92                      b[count2][1]=a[i][1];
 93                      b[count2][2]=a[i][2];
 94                      b[count2][3]=n1;
 95                      count2++;
 96                  }
 97              }
 98             System.out.println("测试结果如下:");
 99             System.out.println("共"+m1+"道题,答对"+count1+"道题,答错"+count2+"道题");
100             System.out.println("错题如下:");
101             for(i=0;i<count2;i++)
102             {
103                 switch(b[i][2])
104                  {
105                  case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+"   ");
106                  break;}
107                  case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+"   ");
108                  break;}
109                  }
110             }
111         }
112         else
113         {
114             System.out.println("请选测数值范围:");
115             int a1=s.nextInt();
116             int a2=s.nextInt();
117             int count=0;
118             while(count<m1)
119             {
120                 boolean m=true;
121                 int s1=(r.nextInt(a2-a1+1)+a1);
122                 int s2=(r.nextInt(a2-a1+1)+a1);
123                 int s3=(r.nextInt(ch.length));
124                 for(i=0;i<=count;i++)
125                 {
126                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
127                     {
128                         m=false;
129                     }
130                 }
131                 if(m=true)
132                 {
133                     a[count][0]=s1;
134                     a[count][1]=s2;
135                     a[count][2]=s3;
136                 }
137                 else
138                 {
139                     count--;
140                 }
141                 count++;
142             }
143             for(i=0;i<m1;i++)
144              {
145                  switch(a[i][2])
146                  {
147                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
148                  break;}
149                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
150                  break;}
151                  }
152                  n1=s.nextInt();
153                  if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1])
154                  {
155                      count1++;
156                  }
157                  else
158                  {
159                      b[count2][0]=a[i][0];
160                      b[count2][1]=a[i][1];
161                      b[count2][2]=a[i][2];
162                      b[count2][3]=n1;
163                      count2++;
164                  }
165              }
166             System.out.println("测试结果如下:");
167             System.out.println("共"+m1+"道题,答对"+count1+"道题,答错"+count2+"道题");
168             System.out.println("错题如下:");
169             for(i=0;i<count2;i++)
170             {
171                 switch(b[i][2])
172                  {
173                  case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+"   ");
174                  break;}
175                  case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+"   ");
176                  break;}
177                  }
178             }
179         }
180     }
181     
182     public static void mul()
183     {              //乘除法
184         System.out.println("******乘除法运算******");
185         int i,count1=0,count2=0;
186         Random r=new Random(); 
187         char[] ch={'+','-','*','/'};//字符数组
188         int a[][]=new int[10000][3];
189         int b[][]=new int[10000][4];
190         Scanner s=new Scanner(System.in);
191         int n1 = 0;
192         System.out.print("请输入算术题的个数:  ");
193         int m1=s.nextInt();
194         //System.out.print("请输入每行打印的个数:  ");
195         //int m3=s.nextInt();
196         System.out.print("加减是否有负数(1/0):");
197         int c2=s.nextInt();
198         if(c2==1)
199         {
200             System.out.println("请选测数值范围:");
201             int a1=s.nextInt();
202             int a2=s.nextInt();
203             int count=0;
204             while(count<m1)
205             {
206                 boolean m=true;
207                 int s1=(r.nextInt(a2-a1+1)+a1);
208                 int s2=(r.nextInt(a2-a1+1)+a1);
209                 int s3=(r.nextInt(ch.length));
210                 for(i=0;i<=count;i++)
211                 {
212                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
213                     {
214                         m=false;
215                     }
216                 }
217                 if(m=true)
218                 {
219                     a[count][0]=s1;
220                     a[count][1]=s2;
221                     a[count][2]=s3;
222                 }
223                 else
224                 {
225                     count--;
226                 }
227                 count++;
228             }
229             for(i=0;i<m1;i++)
230              {
231                  switch(a[i][2])
232                  {
233                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
234                  break;}
235                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
236                  break;}
237                  case 2:{System.out.print(a[i][0]+"*"+a[i][1]+"="+"   ");
238                  break;}
239                  case 3:{System.out.print(a[i][0]+"/"+a[i][1]+"="+"   ");
240                  break;}
241                  }
242                  n1=s.nextInt();
243                  if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1]||n1==a[i][0]*a[i][1]||n1==a[i][0]/a[i][1])
244                  {
245                      count1++;
246                  }
247                  else
248                  {
249                      b[count2][0]=a[i][0];
250                      b[count2][1]=a[i][1];
251                      b[count2][2]=a[i][2];
252                      b[count2][3]=n1;
253                      count2++;
254                  }
255              }
256             System.out.println("测试结果如下:");
257             System.out.println("共"+m1+"道题,答对"+count1+"道题,答错"+count2+"道题");
258             System.out.println("错题如下:");
259             for(i=0;i<count2;i++)
260             {
261                 switch(b[i][2])
262                  {
263                  case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+"   ");
264                  break;}
265                  case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+"   ");
266                  break;}
267                  case 2:{System.out.print(b[i][0]+"*"+b[i][1]+"="+b[i][3]+"   ");
268                  break;}
269                  case 3:{System.out.print(b[i][0]+"/"+b[i][1]+"="+b[i][3]+"   ");
270                  break;}
271                  }
272             }
273 
274         }
275         else
276         {
277             System.out.println("请选测数值范围:");
278             int a1=s.nextInt();
279             int a2=s.nextInt();
280             int count=0;
281             while(count<m1)
282             {
283                 boolean m=true;
284                 int s1=(r.nextInt(a2-a1+1)+a1);
285                 int s2=(r.nextInt(a2-a1+1)+a1);
286                 int s3=(r.nextInt(ch.length));
287                 for(i=0;i<=count;i++)
288                 {
289                     if(a[i][0]==s1&&a[i][1]==s2&&a[i][2]==s3)
290                     {
291                         m=false;
292                     }
293                 }
294                 if(m=true)
295                 {
296                     a[count][0]=s1;
297                     a[count][1]=s2;
298                     a[count][2]=s3;
299                 }
300                 else
301                 {
302                     count--;
303                 }
304                 count++;
305             }
306             for(i=0;i<m1;i++)
307              {
308                  switch(a[i][2])
309                  {
310                  case 0:{System.out.print(a[i][0]+"+"+a[i][1]+"="+"   ");
311                  break;}
312                  case 1:{System.out.print(a[i][0]+"-"+a[i][1]+"="+"   ");
313                  break;}
314                  case 2:{System.out.print(a[i][0]+"*"+a[i][1]+"="+"   ");
315                  break;}
316                  case 3:{System.out.print(a[i][0]+"/"+a[i][1]+"="+"   ");
317                  break;}
318                  }
319                  n1=s.nextInt();
320                  if(n1==a[i][0]+a[i][1]||n1==a[i][0]-a[i][1]||n1==a[i][0]*a[i][1]||n1==a[i][0]/a[i][1])
321                  {
322                      count1++;
323                  }
324                  else
325                  {
326                      b[count2][0]=a[i][0];
327                      b[count2][1]=a[i][1];
328                      b[count2][2]=a[i][2];
329                      b[count2][3]=n1;
330                      count2++;
331                  }
332              }
333             System.out.println("测试结果如下:");
334             System.out.println("共"+m1+"道题,答对"+count1+"道题,答错"+count2+"道题");
335             System.out.println("错题如下:");
336             for(i=0;i<count2;i++)
337             {
338                 switch(b[i][2])
339                  {
340                  case 0:{System.out.print(b[i][0]+"+"+b[i][1]+"="+b[i][3]+"   ");
341                  break;}
342                  case 1:{System.out.print(b[i][0]+"-"+b[i][1]+"="+b[i][3]+"   ");
343                  break;}
344                  case 2:{System.out.print(b[i][0]+"*"+b[i][1]+"="+b[i][3]+"   ");
345                  break;}
346                  case 3:{System.out.print(b[i][0]+"/"+b[i][1]+"="+b[i][3]+"   ");
347                  break;}
348                  }
349             }
350         }
351     }
352 }
原文地址:https://www.cnblogs.com/wanghaoning/p/14143980.html