软件工程个人作业02(四则运算)

四则运算作业02

import javax.swing.JOptionPane;

import java.util.Random;
import java.util.Scanner;
public class Arithmetic {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        int c;//选择
        int c1,c4,c5,c6,c7,c8;
        int a,b,d1;//二位数运算
        int c2,c3;
        int e=0;//运算符
        String s="";
        double d=0.0;//2位数的结果,用户
        double f=0.0,g=0.0;//2位数结果,系统,多位数结果,系统
        int flag=0;//判断是否输出
        int m=0;//题数
        Random rand = new Random();
        for(int i=0;i<100000000;i++)
            {
                System.out.println("请输入选择:1、2位数运算  2、多位数运算 3、退出");
                Scanner input=new Scanner(System.in) ;
                c=input.nextInt();
                if(c==1)
                {
                    System.out.println("请输入选择:1 、有乘除法 2、无乘除法");
                     Scanner input1=new Scanner(System.in) ;
                     c1=input1.nextInt();
                     System.out.println("请输入数值范围的前域 ");
                     Scanner input2=new Scanner(System.in) ;
                     c2=input2.nextInt();
                     System.out.println("请输入数值范围的后域");
                     Scanner input3=new Scanner(System.in) ;
                     c3=input3.nextInt();
                     System.out.println("请输入选择:1、加减有负数 2、加减无负数");
                     Scanner input4=new Scanner(System.in) ;
                     c4=input4.nextInt();
                     System.out.println("请输入选择:1、除法有余数 2、除法无余数");
                     Scanner input5=new Scanner(System.in) ;
                     c5=input5.nextInt();
                     System.out.println("请输入出题数量");
                     Scanner input6=new Scanner(System.in) ;
                     c6=input6.nextInt();
                     System.out.println("请输入在一行中输出几列运算式?");
                     Scanner input7=new Scanner(System.in) ;
                     c7=input7.nextInt();
                        String []Repeat=new String[2*c6];
                     for(int w=0;w<c6;w++)
                     {
                         int w1;
                         w1=w;
                        //有无乘除法
                         if(c1==1)
                         {
                             e=rand.nextInt(4);
                         }
                         if(c1==2)
                         {
                             e=rand.nextInt(2);
                         }
                         //数值范围
                         a=rand.nextInt(c3)%(c3-c2+1)+c2;
                         b=rand.nextInt(c3)%(c3-c2+1)+c2;
                         //加减有无负数
                         if(c4==1)//有负数
                         {
                             flag=0;
                         }
                         if(c4==2)
                         { 
                             if(e==0)
                             {
                                 if((a+b)>=0)
                                 {
                                     flag=0;
                                 }
                                 else 
                                 {
                                     flag=1;
                                 }
                             }
                             if(e==1)
                             {
                                 if((a-b)>=0)
                                 {
                                     flag=0;
                                 }
                                 else
                                 {
                                     flag=1;
                                 }
                             }
                         }
                        //符号
                         if(e==0)
                         {
                             s="+";
                         }
                         if(e==1)
                         {
                             s="-";
                         }
                         if(e==2)
                         {
                             s="*";
                         }
                         if(e==3)
                         {
                             if(b!=0)
                             {
                                 if(c5==1)
                                 {
                                    s="/"; 
                                 }
                                 if(c5==2)
                                 {
                                     if(a%b==0)
                                     {
                                         s="/";
                                     }
                                     if(a%b!=0)
                                     {
                                         flag=1;
                                     }
                                 }
                                 
                             }
                         }
                         //判断重复
                         if(a<0.0&&b>=0.0)
                         {
                             Repeat[w]="("+a+")"+s+b;
                         }
                         if(a>=0.0&&b<0.0)
                         {
                             Repeat[w]=a+s+"("+b+")";
                         }
                         if(a<0.0&&b<0.0)
                         {
                             Repeat[w]="("+a+")"+s+"("+b+")";
                         }
                         if(a>=0.0&&b>=0.0)
                         {
                             Repeat[w]=a+s+b;
                         }
                         for(int w2=0;w2<w1;w2++)
                         {
                             if(Repeat[w].equals(Repeat[w2]))
                             {
                                 flag =1;
                             }
                             else
                             {flag =0;}
                         }
                         //打印
                         if(flag==0)
                         {
                             if((w+1)%c7==0)
                               {
                                   System.out.println(Repeat[w]+"=  ");
                               }
                               else
                               {
                                   System.out.print(Repeat[w]+"=  "); 
                               }
                         }
                         if(flag==1)
                         {
                             c6++;
                         }
                         
                     }
                  }
           if(c==2)
           {
               int y1,y2,y3,y;
               System.out.println("请输入选择:1 、有乘除法 2、无乘除法");
                 Scanner input1=new Scanner(System.in) ;
                 c1=input1.nextInt();
                 System.out.println("请输入数值范围的前域 ");
                 Scanner input2=new Scanner(System.in) ;
                 c2=input2.nextInt();
                 System.out.println("请输入数值范围的后域");
                 Scanner input3=new Scanner(System.in) ;
                 c3=input3.nextInt();
                 System.out.println("请输入选择:1、加减有负数 2、加减无负数");
                 Scanner input4=new Scanner(System.in) ;
                 c4=input4.nextInt();
                 System.out.println("请输入选择:1、除法有余数 2、除法无余数");
                 Scanner input5=new Scanner(System.in) ;
                 c5=input5.nextInt();
                 System.out.println("请输入出题数量");
                 Scanner input6=new Scanner(System.in) ;
                 c6=input6.nextInt();
                 System.out.println("请输入在一行中输出几列运算式?");
                 Scanner input7=new Scanner(System.in) ;
                 c7=input7.nextInt();
                 System.out.println("请输入选择: 1、有括号 2、无括号");
                 Scanner input8=new Scanner(System.in) ;
                 c8=input8.nextInt();
             //定义数组
                 String []Repeat=new String[2*c6];
                  /*设置出题多少的循环*/
                 for(int w=0;w<c6;w++)
                {
                     int w1;
                     w1=w;
                    // w4++;//用于换行的变量

                  //有无乘除法
                  if(c1==1)
                  {
                    e=rand.nextInt(4);
                  }
                  else if(c1==2)
                  {
                      e=rand.nextInt(2);
                  }
                  //数值范围
                   a=rand.nextInt(c3)%(c3-c2+1)+c2;
                   b=rand.nextInt(c3)%(c3-c2+1)+c2;
                  
                  //加减有无负数
                   if(c4==1)//有负数
                   {
                       flag=0;
                   }
                   if(c4==2)//无负数
                   {
                       
                       y1=a+b;
                       y2=a-b;
                       if(e==0)
                       {
                           if(y1<0)
                           {
                               flag=1;
                           }
                       }
                       else if(e==1)
                       {
                           if(y2<0)
                           {
                               flag=1;
                           }
                       }
                   }
                   //除法有无余数
                   if(c5==1)
                   {
                     flag=0;
                   }
                   if(c5==2)
                   {
                      y3=a%b;
                      if(y3!=0)
                      {
                          flag=1;
                      }
                   }
                    
                   //符号
                     if(e==0)
                     {
                        s="+"; 
                     }
                     if(e==1)
                     {
                         s="-";
                     }
                     if(e==2)
                     {
                         s="*";
                     }
                     if(e==3)
                     {
                      
                         if(d==0)
                         {flag=1;}
                         if(d!=0)
                         {
                         s="/";
                         }
                     }
                   
                   
                   Repeat[w]=a+s+b; 
                     
                     //有无括号
                      y=rand.nextInt(8);
                      int p;//随机出数,选择在原来的式子之前加数还是之后

                     if(c8==1)
                     {
                         
                           for(int r=0;r<(y-2);r++) 
                           {
                               c=rand.nextInt(c3)%(c3-c2+1)+c2;
                                d=rand.nextInt(c3)%(c3-c2+1)+c2;

                               if(c1==1)
                             {
                               e=rand.nextInt(4);
                             }
                             else if(c1==2)
                             {
                                 e=rand.nextInt(2);
                             } 
                          
                             //符号
                             if(e==0)
                             {
                                s="+"; 
                             }
                             if(e==1)
                             {
                                 s="-";
                             }
                             if(e==2)
                             {
                                 s="*";
                             }
                             if(e==3)
                             {
                              
                                 if(d==0)
                                 {flag=1;}
                                 if(d!=0)
                                 {
                                 s="/";
                                 }
                             }
                              
                                p=rand.nextInt(2);
                               if(p==0)
                               {
                                  Repeat[w]=c+s+"("+Repeat[w]+")";
                               }
                               if(p==1)
                               {
                                  Repeat[w]="("+Repeat[w]+")"+s+d;
                               }
                           }
                     }
                        if(c8==2)
                     {
                            for(int r=0;r<(y-2);r++) 
                            {
                               c=rand.nextInt(c3)%(c3-c2+1)+c2;
                             d=rand.nextInt(c3)%(c3-c3+1)+c3;

                           if(c1==1)
                            {
                               e=rand.nextInt(4);
                            }
                            else if(c1==2)
                            {
                               e=rand.nextInt(2);
                            } 
                    
                           //符号
                          if(e==0)
                          {
                             s="+"; 
                          }
                          if(e==1)
                          {
                            s="-";
                          }
                          if(e==2)
                          {
                             s="*";
                          }
                         if(e==3)
                         {
                        
                           if(d==0)
                           {flag=1;}
                           if(d!=0)
                           {
                                s="/";
                           }
                       }
                        
                                p=rand.nextInt(1);
                               if(p==0)
                               {
                                  Repeat[w]=c+s+Repeat[w];
                               }
                               if(p==1)
                               {
                                   Repeat[w]=Repeat[w]+s+d;
                               }
                            }
                     }
                        
                        
                        
                   //判断重复
                       for(int w2=0;w2<w1;w2++)
                     {
                         if(Repeat[w].equals(Repeat[w2]))
                         {
                             flag =1;
                         }
                         else
                         {flag =0;}
                     }
                   if(flag==0)
                    {   
                       if((w+1)%c7==0)
                       {
                           System.out.println(Repeat[w]+"=");
                       }
                       else
                       {
                        System.out.print(Repeat[w]+"=  ");
                       }                    
                    }
                    else if(flag==1)
                    {c6++;}   
               }//for   
           }
           if(c==3)
            {break;}
        }
    }

}


  截图:

原文地址:https://www.cnblogs.com/CkmIT/p/6544661.html