个人作业四则运算2测试

  1 import javax.swing.JOptionPane;
  2 
  3 import java.util.Random;
  4 import java.util.Scanner;
  5 public class Arithmetic {
  6 
  7     public static void main(String[] args) {
  8         // TODO 自动生成的方法存根
  9         int c;//选择
 10         int c1,c4,c5,c6,c7,c8;
 11         int a,b,d1;//二位数运算
 12         int c2,c3;
 13         int e=0;//运算符
 14         String s="";
 15         double d=0.0;//2位数的结果,用户
 16         double f=0.0,g=0.0;//2位数结果,系统,多位数结果,系统
 17         int flag=0;//判断是否输出
 18         int m=0;//题数
 19         Random rand = new Random();
 20         for(int i=0;i<100000000;i++)
 21             {
 22                 System.out.println("请输入选择:1、2位数运算  2、多位数运算 3、退出");
 23                 Scanner input=new Scanner(System.in) ;
 24                 c=input.nextInt();
 25                 if(c==1)
 26                 {
 27                     System.out.println("请输入选择:1 、有乘除法 2、无乘除法");
 28                      Scanner input1=new Scanner(System.in) ;
 29                      c1=input1.nextInt();
 30                      System.out.println("请输入数值范围的前域 ");
 31                      Scanner input2=new Scanner(System.in) ;
 32                      c2=input2.nextInt();
 33                      System.out.println("请输入数值范围的后域");
 34                      Scanner input3=new Scanner(System.in) ;
 35                      c3=input3.nextInt();
 36                      System.out.println("请输入选择:1、加减有负数 2、加减无负数");
 37                      Scanner input4=new Scanner(System.in) ;
 38                      c4=input4.nextInt();
 39                      System.out.println("请输入选择:1、除法有余数 2、除法无余数");
 40                      Scanner input5=new Scanner(System.in) ;
 41                      c5=input5.nextInt();
 42                      System.out.println("请输入出题数量");
 43                      Scanner input6=new Scanner(System.in) ;
 44                      c6=input6.nextInt();
 45                      System.out.println("请输入在一行中输出几列运算式?");
 46                      Scanner input7=new Scanner(System.in) ;
 47                      c7=input7.nextInt();
 48                         String []Repeat=new String[2*c6];
 49                      for(int w=0;w<c6;w++)
 50                      {
 51                          int w1;
 52                          w1=w;
 53                         //有无乘除法
 54                          if(c1==1)
 55                          {
 56                              e=rand.nextInt(4);
 57                          }
 58                          if(c1==2)
 59                          {
 60                              e=rand.nextInt(2);
 61                          }
 62                          //数值范围
 63                          a=rand.nextInt(c3)%(c3-c2+1)+c2;
 64                          b=rand.nextInt(c3)%(c3-c2+1)+c2;
 65                          //加减有无负数
 66                          if(c4==1)//有负数
 67                          {
 68                              flag=0;
 69                          }
 70                          if(c4==2)
 71                          { 
 72                              if(e==0)
 73                              {
 74                                  if((a+b)>=0)
 75                                  {
 76                                      flag=0;
 77                                  }
 78                                  else 
 79                                  {
 80                                      flag=1;
 81                                  }
 82                              }
 83                              if(e==1)
 84                              {
 85                                  if((a-b)>=0)
 86                                  {
 87                                      flag=0;
 88                                  }
 89                                  else
 90                                  {
 91                                      flag=1;
 92                                  }
 93                              }
 94                          }
 95                         //符号
 96                          if(e==0)
 97                          {
 98                              s="+";
 99                          }
100                          if(e==1)
101                          {
102                              s="-";
103                          }
104                          if(e==2)
105                          {
106                              s="*";
107                          }
108                          if(e==3)
109                          {
110                              if(b!=0)
111                              {
112                                  if(c5==1)
113                                  {
114                                     s="/"; 
115                                  }
116                                  if(c5==2)
117                                  {
118                                      if(a%b==0)
119                                      {
120                                          s="/";
121                                      }
122                                      if(a%b!=0)
123                                      {
124                                          flag=1;
125                                      }
126                                  }
127                                  
128                              }
129                          }
130                          //判断重复
131                          if(a<0.0&&b>=0.0)
132                          {
133                              Repeat[w]="("+a+")"+s+b;
134                          }
135                          if(a>=0.0&&b<0.0)
136                          {
137                              Repeat[w]=a+s+"("+b+")";
138                          }
139                          if(a<0.0&&b<0.0)
140                          {
141                              Repeat[w]="("+a+")"+s+"("+b+")";
142                          }
143                          if(a>=0.0&&b>=0.0)
144                          {
145                              Repeat[w]=a+s+b;
146                          }
147                          for(int w2=0;w2<w1;w2++)
148                          {
149                              if(Repeat[w].equals(Repeat[w2]))
150                              {
151                                  flag =1;
152                              }
153                              else
154                              {flag =0;}
155                          }
156                          //打印
157                          if(flag==0)
158                          {
159                              if((w+1)%c7==0)
160                                {
161                                    System.out.println(Repeat[w]+"=  ");
162                                }
163                                else
164                                {
165                                    System.out.print(Repeat[w]+"=  "); 
166                                }
167                          }
168                          if(flag==1)
169                          {
170                              c6++;
171                          }
172                          
173                      }
174                   }
175            if(c==2)
176            {
177                int y1,y2,y3,y;
178                System.out.println("请输入选择:1 、有乘除法 2、无乘除法");
179                  Scanner input1=new Scanner(System.in) ;
180                  c1=input1.nextInt();
181                  System.out.println("请输入数值范围的前域 ");
182                  Scanner input2=new Scanner(System.in) ;
183                  c2=input2.nextInt();
184                  System.out.println("请输入数值范围的后域");
185                  Scanner input3=new Scanner(System.in) ;
186                  c3=input3.nextInt();
187                  System.out.println("请输入选择:1、加减有负数 2、加减无负数");
188                  Scanner input4=new Scanner(System.in) ;
189                  c4=input4.nextInt();
190                  System.out.println("请输入选择:1、除法有余数 2、除法无余数");
191                  Scanner input5=new Scanner(System.in) ;
192                  c5=input5.nextInt();
193                  System.out.println("请输入出题数量");
194                  Scanner input6=new Scanner(System.in) ;
195                  c6=input6.nextInt();
196                  System.out.println("请输入在一行中输出几列运算式?");
197                  Scanner input7=new Scanner(System.in) ;
198                  c7=input7.nextInt();
199                  System.out.println("请输入选择: 1、有括号 2、无括号");
200                  Scanner input8=new Scanner(System.in) ;
201                  c8=input8.nextInt();
202              //定义数组
203                  String []Repeat=new String[2*c6];
204                   /*设置出题多少的循环*/
205                  for(int w=0;w<c6;w++)
206                 {
207                      int w1;
208                      w1=w;
209                     // w4++;//用于换行的变量
210 
211                   //有无乘除法
212                   if(c1==1)
213                   {
214                     e=rand.nextInt(4);
215                   }
216                   else if(c1==2)
217                   {
218                       e=rand.nextInt(2);
219                   }
220                   //数值范围
221                    a=rand.nextInt(c3)%(c3-c2+1)+c2;
222                    b=rand.nextInt(c3)%(c3-c2+1)+c2;
223                   
224                   //加减有无负数
225                    if(c4==1)//有负数
226                    {
227                        flag=0;
228                    }
229                    if(c4==2)//无负数
230                    {
231                        
232                        y1=a+b;
233                        y2=a-b;
234                        if(e==0)
235                        {
236                            if(y1<0)
237                            {
238                                flag=1;
239                            }
240                        }
241                        else if(e==1)
242                        {
243                            if(y2<0)
244                            {
245                                flag=1;
246                            }
247                        }
248                    }
249                    //除法有无余数
250                    if(c5==1)
251                    {
252                      flag=0;
253                    }
254                    if(c5==2)
255                    {
256                       y3=a%b;
257                       if(y3!=0)
258                       {
259                           flag=1;
260                       }
261                    }
262                     
263                    //符号
264                      if(e==0)
265                      {
266                         s="+"; 
267                      }
268                      if(e==1)
269                      {
270                          s="-";
271                      }
272                      if(e==2)
273                      {
274                          s="*";
275                      }
276                      if(e==3)
277                      {
278                       
279                          if(d==0)
280                          {flag=1;}
281                          if(d!=0)
282                          {
283                          s="/";
284                          }
285                      }
286                    
287                    
288                    Repeat[w]=a+s+b; 
289                      
290                      //有无括号
291                       y=rand.nextInt(8);
292                       int p;//随机出数,选择在原来的式子之前加数还是之后
293 
294                      if(c8==1)
295                      {
296                          
297                            for(int r=0;r<(y-2);r++) 
298                            {
299                                c=rand.nextInt(c3)%(c3-c2+1)+c2;
300                                 d=rand.nextInt(c3)%(c3-c2+1)+c2;
301 
302                                if(c1==1)
303                              {
304                                e=rand.nextInt(4);
305                              }
306                              else if(c1==2)
307                              {
308                                  e=rand.nextInt(2);
309                              } 
310                           
311                              //符号
312                              if(e==0)
313                              {
314                                 s="+"; 
315                              }
316                              if(e==1)
317                              {
318                                  s="-";
319                              }
320                              if(e==2)
321                              {
322                                  s="*";
323                              }
324                              if(e==3)
325                              {
326                               
327                                  if(d==0)
328                                  {flag=1;}
329                                  if(d!=0)
330                                  {
331                                  s="/";
332                                  }
333                              }
334                               
335                                 p=rand.nextInt(2);
336                                if(p==0)
337                                {
338                                   Repeat[w]=c+s+"("+Repeat[w]+")";
339                                }
340                                if(p==1)
341                                {
342                                   Repeat[w]="("+Repeat[w]+")"+s+d;
343                                }
344                            }
345                      }
346                         if(c8==2)
347                      {
348                             for(int r=0;r<(y-2);r++) 
349                             {
350                                c=rand.nextInt(c3)%(c3-c2+1)+c2;
351                              d=rand.nextInt(c3)%(c3-c3+1)+c3;
352 
353                            if(c1==1)
354                             {
355                                e=rand.nextInt(4);
356                             }
357                             else if(c1==2)
358                             {
359                                e=rand.nextInt(2);
360                             } 
361                     
362                            //符号
363                           if(e==0)
364                           {
365                              s="+"; 
366                           }
367                           if(e==1)
368                           {
369                             s="-";
370                           }
371                           if(e==2)
372                           {
373                              s="*";
374                           }
375                          if(e==3)
376                          {
377                         
378                            if(d==0)
379                            {flag=1;}
380                            if(d!=0)
381                            {
382                                 s="/";
383                            }
384                        }
385                         
386                                 p=rand.nextInt(1);
387                                if(p==0)
388                                {
389                                   Repeat[w]=c+s+Repeat[w];
390                                }
391                                if(p==1)
392                                {
393                                    Repeat[w]=Repeat[w]+s+d;
394                                }
395                             }
396                      }
397                         
398                         
399                         
400                    //判断重复
401                        for(int w2=0;w2<w1;w2++)
402                      {
403                          if(Repeat[w].equals(Repeat[w2]))
404                          {
405                              flag =1;
406                          }
407                          else
408                          {flag =0;}
409                      }
410                    if(flag==0)
411                     {   
412                        if((w+1)%c7==0)
413                        {
414                            System.out.println(Repeat[w]+"=");
415                        }
416                        else
417                        {
418                         System.out.print(Repeat[w]+"=  ");
419                        }                    
420                     }
421                     else if(flag==1)
422                     {c6++;}   
423                }//for   
424            }
425            if(c==3)
426             {break;}
427         }
428     }
429 
430 }

2位数,有乘除法,负数,余数,

2位数,无乘除法,负数,余数

2位数,有乘除法,无负数,余数

多位数,有乘除法,有括号

多位数,无乘除法,无括号

3,退出

原文地址:https://www.cnblogs.com/muamu/p/5322787.html