四则运算2测试

代码:

import java.util.*;

public class Aritmetic1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要生成多少道四则运算题目?");
        int h = input.nextInt();
        String [] arr1 = new String[h];
        System.out.println("请输入随机数可产生的最大值:");
        int g = input.nextInt();
        System.out.println("请输入定制要求(每行题数):");
        int r = input.nextInt();
        System.out.println("请选择要求:");
        System.out.println("1加减法运算");
        System.out.println("2四则运算");
        Random random = new Random();
        char arr[] = {'+','-','*','/','('};
        
        int a=input.nextInt();
        switch(a){
           case 1:{
              System.out.println("请选择数字类型:");
              System.out.println("1整数");
              System.out.println("2分数");
              int m = input.nextInt();
              switch(m)
              {
              case 1:
              {
                  System.out.println("有无负数?");
                  System.out.println("1没有负数");
                  System.out.println("2有负数");
                  int f = input.nextInt(); 
                  int i;
                  switch(f)
                  {
                     case 1:
                     {
                         for(i=0;i<h;i++)
                         {
                             if(i==r||i%r==0)
                             {
                                 System.out.println();
                             }
                              String str = random.nextInt(g) + " " + arr[random.nextInt(2)] + " " + random.nextInt(g) + "="+"     ";
                              System.out.print(str);
                             
                         }
                         break;
                     }
                     
                     case 2:
                     {
                         for(i=0;i<h;i++)
                         {
                             if(i==r||i%r==0)
                             {
                                 System.out.println();
                             }
                             String str = (-random.nextInt()%g) + " " + arr[random.nextInt(2)] + " " + (-random.nextInt()%g) + "="+"     ";
                             System.out.print(str);
                         }
                         break;
                     }
                     
                     default:
                     {
                         System.out.println("输入错误,请输入1或2");
                     }
                     break;
                  }//switch(f)
                  break;
              }//case1内
              
              case 2:
              {
                  System.out.println("有无负数?");
                  System.out.println("1没有负数");
                  System.out.println("2有负数");
                     int f=input.nextInt();
                     switch(f)
                     {
                     case 1:
                     {
                         for(int i=0;i<h;i++)
                         {
                             if(i==r||i%r==0)
                             {
                                 System.out.println();
                             }
                            System.out.print(random.nextInt(g)+"/"+random.nextInt(g)+1+" "+arr[random.nextInt(2)]+" "+random.nextInt(g)+"/"+random.nextInt(g)+1+"="+"     ");
                         }
                         break;
                     }
                     case 2:
                     {
                         for(int i=0;i<h;i++)
                         {
                             if(i==r||i%r==0)
                             {
                                 System.out.println();
                             }
                            System.out.print(-random.nextInt()%g+"/"+random.nextInt(g)+1+" "+arr[random.nextInt(2)]+" "+-random.nextInt()%g+"/"+random.nextInt(g)+1+"="+"     ");
                         }
                         break;
                     }
                     default:
                         System.out.println("输入错误,请输入1或2");
                         break;
                     }
                    break;
              }//case2内
              }//switch(m)
            break;
           }//case1外
           
           case 2:
           {
              System.out.println("请选择数字类型:");
              System.out.println("1整数");
              System.out.println("2分数");
              int m = input.nextInt();
              switch(m)
              {
              case 1:
              {
                  System.out.println("请选择要求:");
                  System.out.println("1正整数无余数四则运算");
                  System.out.println("2可负整数无余数四则运算");
                  System.out.println("3正整数可有余数四则运算");
                  System.out.println("4可负整数可有余数四则运算");
                  System.out.println("5多数带括号运算");
                  int f =input.nextInt();
                  switch(f)
                  {
                  case 1:
                  {
                         for(int i=0;i<h;i++)
                         {
                             
                             int u=random.nextInt(g);
                             int v=random.nextInt(g)+1;
                             char w=arr[random.nextInt(4)];
                             if(i==r||i%r==0)
                           {
                               System.out.println();
                           }
                             if(u%v==0 && w=='/')
                             {
                                 System.out.print(u+" "+w+" "+v+"="+"     ");
                             }
                             else
                             {
                                 System.out.print(u+" "+arr[random.nextInt(3)]+" "+v+"="+"     ");
                             }
                         }
                         break;
                  }
                  
                  case 2:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int u=-random.nextInt()%g;
                             int v=-random.nextInt()%g;
                             char w=arr[random.nextInt(4)];
                             if(i==r||i%r==0)
                             {
                                 System.out.println();
                             }
                             if(v!=0 && u%v==0 && w=='/' )
                                 System.out.print(u+" "+w+" "+v+"="+"     ");
                            else
                                System.out.print(u+" "+arr[random.nextInt(3)]+" "+v+"="+"     ");
                         }
                         break;
                     }
                  case 3:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int u=random.nextInt(g);
                             int v=random.nextInt(g)+1;
                             char w=arr[random.nextInt(4)];
                             if(i==r||i%r==0)
                             {
                                 System.out.println();
                             }
                             System.out.print(u+" "+w+" "+v+"="+"     ");
                         }
                         break;
                     }
                  case 4:
                     {
                         for(int i=0;i<h;i++)
                         {
                         int u=-random.nextInt()%g;
                         int v=-random.nextInt()%g;
                         char w=arr[random.nextInt(4)];
                         if(i==r||i%r==0)
                         {
                             System.out.println();
                         }
                         System.out.print(u+" "+w+" "+v+"="+"     ");
                         }
                         break;
                     }
                  case 5:
                  {
                      for(int i=0;i<h;i++)
                      {
                          int n =random.nextInt(9)+2;
                          Stack<Integer> S1 = new Stack<Integer>();
                          Stack<Character> S2 = new Stack<Character>();
                          for(int j=0;j<n+1;j++)
                          {
                              char w=' ';
                              int e = random.nextInt()%g;
                              if(w=='/'&&e==0)
                              {
                                  e=random.nextInt(g)+1;
                              }
                              w=arr[random.nextInt(5)];
                              S1.push(e);
                              S2.push(w);
                          }
                          
                          int k=0;
                          for(int j=0;j<n;j++)
                          {
                              char q =S2.pop();
                              if(q=='(')
                              {
                                  if(j>n-2)
                                  {
                                      System.out.print(S1.pop()+" "+arr[random.nextInt(4)]);
                                  }
                                  else
                                  {
                                      System.out.print(S1.pop()+" "+arr[random.nextInt(4)]+" "+q);
                                      k++;
                                  }
                              }
                              else
                              {
                                  System.out.print(S1.pop()+" "+q);
                              }
                          }
                          if(k==0)
                          {
                              System.out.println(random.nextInt()%g+"=");
                          }
                          else if(k==1)
                          {
                              System.out.println(random.nextInt()%g+")=");
                          }
                          else if(k==2)
                          {
                              System.out.println(random.nextInt()%g+"))=");
                          }
                          else if(k==3)
                          {
                              System.out.println(random.nextInt()%g+")))=");
                          }
                          else
                          {
                              System.out.println(random.nextInt()%g+"))))=");
                          }
                      }
                      break;
                  }
                     default:
                     {
                         System.out.println("输入错误,请输入1-5");
                     }
                     break;
                  }//switch(f)
                  break;
              }//case1内
              
              case 2:
              {
                    System.out.println("请选择要求:");
                    System.out.println("1正分数四则运算!");
                    System.out.println("2可负分数四则运算!");
                    int f=input.nextInt();
                     switch(f)
                     {
                     case 1:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int u=random.nextInt(g);
                             int v=random.nextInt(g)+1;
                             int x=random.nextInt(g);
                             int y=random.nextInt(g)+1;
                             char w=arr[random.nextInt(4)];
                             if(i==r||i%r==0)
                             {
                                 System.out.println();
                             }
                            System.out.print(u+"/"+v+" "+w+" "+x+"/"+y+"="+"     ");
                         }
                         break;
                     }
                     case 2:
                     {
                         for(int i=0;i<h;i++)
                         {
                             int u=-random.nextInt()%g;
                             int v=random.nextInt(g)+1;
                             int x=-random.nextInt()%g;
                             int y=random.nextInt(g)+1;
                             char w=arr[random.nextInt(4)];
                             if(i==r||i%r==0)
                             {
                                 System.out.println();
                             }
                             if(x!=0 && w=='/' )
                                 System.out.print(u+"/"+v+" "+w+" "+x+"/"+y+"="+"     ");
                            else 
                                System.out.print(u+"/"+v+" "+arr[random.nextInt(3)]+" "+x+"/"+y+"="+"     ");
                         }
                         break;
                     }
                     default:
                         System.out.print("输入的数有误,请输入1或2");
                     }
                     break;
                }//case2内
              }
              }//switch(m)
           }//case2外
        }//switch(a)

    }//起始

测试:

①正常输出

②扩大范围

③是否有负分数

④十数以内

⑤分母为零

⑥可订制

 

posted @ 2016-03-26 09:55  mrferry  阅读(237)  评论(0编辑  收藏  举报