• 软件工程个人作业02


    写一个能自动生成三十道小学四则运算题目的 “软件”,新的要求: 可以控制下列参数: 是否有乘除法; 是否有括号(最多可以支持十个数参与计算); 数值范围; 加减有无负数; 除法有无余数!

    1.设计思想

    将整数与真分数分开讨论。将计算结果和式子分别保存到两个数组,通过控制条件来筛选满足的随机数组成的式子,并与用户输入的结果匹配来判断对错。

    2.源代码

    import java.util.Scanner;
    
    public class Szys {
    
        public static void main(String[] args) {
            System.out.println("请输入要生成的题数:");
            Scanner N = new Scanner(System.in);
            int n=N.nextInt();
            System.out.println("请输入出题的类型:(1.整数 2.真分数)");
            Scanner N1 = new Scanner(System.in);
            int n1=N1.nextInt();
            System.out.println("注:除法请填写化简之后的结果(分母为1时也要写出)");
            
            System.out.println("请输入数字的取值范围(起始和结束的正整数):");
            Scanner N5 = new Scanner(System.in);
            int n5=N5.nextInt();
            Scanner N6 = new Scanner(System.in);
            int n6=N6.nextInt();
            
            if(n1==1)  zheng(n5,n6,n);
            else if(n1==2)  zhenfs(n5,n6,n);
            else System.out.println("输入错误!");
        }
        
        public static int random(int w,int v)
        {
            int value=w + (int) ( Math.random() * v );
            return value;
        }
        public static void jisuan1(int n,int value1,int value2,int ys,int i)
        {
            String ys1;
            String result[] =new String[n];
            String output[] =new String[n];
            
            if(ys==1) {ys1="+";result[i]=value1+value2+"";}
            else if(ys==2) {ys1="-";result[i]=(value1-value2)+"";}
            else if(ys==3) {ys1="*";result[i]=(value1*value2)+"";}
            else {ys1="/";result[i]=yuefen(value1,value2);}
            output[i]= value1 + " "+ys1 +" "+ value2+"=";
            System.out.println(output[i]);
            
            Scanner N2 = new Scanner(System.in);
            String n2=N2.nextLine();
            if(n2.equals(result[i]))
                 System.out.println("回答正确!");
            else System.out.println("回答错误!");
        }
        
        public static void jisuan2(int n,int value1,int value2,int value3,int value4,int ys,int i)
        {
            String ys1;
            String result[] =new String[n];
            String output[] =new String[n];
            if(ys==1) {ys1="+";result[i]=yuefen(((value1*value4)+(value2*value3)),value2*value4)+"";}
            else if(ys==2) {ys1="-";result[i]=yuefen(((value1*value4)-(value2*value3)),value2*value4)+"";}
            else if(ys==3) {ys1="*";result[i]=yuefen((value1*value3),(value2*value4))+"";}
            else {ys1="/";result[i]=yuefen((value1*value4),(value2*value3))+"";}
            
            output[i]= yuefen(value1,value2)+" "+ys1+" "+yuefen(value3,value4)+"=";
            System.out.println(output[i]);
               
            Scanner N2 = new Scanner(System.in);
            String n2=N2.nextLine();
            if(n2.equals(result[i]))
                 System.out.println("回答正确!");
            else System.out.println("回答错误!");
        }
        
         public static String yuefen(int a1,int a2)
         {
             int min=a1<a2?a1:a2;
             int yueshu=1;
             for(int i=min;i>=1;i--)
             {
                 if(a1%i==0 && a2%i==0)
                 {
                     yueshu=i;
                     break;
                 }
             }
             a1/=yueshu;
             a2/=yueshu;
             return(a1+"/"+a2);
         }
        
        public static void zheng(int x,int y,int n)
        {
            int value1,value2;
            int ys;
            String ys1;
            
            System.out.println("请选择加减有无负数(1.有 2.无):");
            Scanner N3 = new Scanner(System.in);
            int n3=N3.nextInt();
            
            System.out.println("请选择是否有乘除法(1.有 2.无):");
            Scanner N4 = new Scanner(System.in);
            int n4=N4.nextInt();
            
            System.out.println("请选择除法有无余数(1.有 2.无):");
            Scanner N7 = new Scanner(System.in);
            int n7=N7.nextInt();
               
            for ( int i = 0; i < n; i++ ) 
            {
               value1 = random(x,y);
               value2 = random(x,y);
               
               if(n3==1&&n4==1&&n7==1)//加减有负数,有乘除法,有余数
               {
                   ys = random(1,4);
                   if(value1%value2!=0)
                      jisuan1(n,value1,value2,ys,i);
                   else n++;
               }
               else if(n3==1&&n4==1&&n7==2)//加减有负数,有乘除法,无余数
               {
                   ys = random(1,4);
                   if(value1>=value2&&value1%value2==0)
                      jisuan1(n,value1,value2,ys,i);
                   else n++;
               }
               else if(n3==1&&n4==2)//无乘除法
               {
                   ys = random(1,2);
                   jisuan1(n,value1,value2,ys,i);
               }
               else if(n3==2)//加减无负数
               {
                   if(value1>=value2&&n4==1&&n7==1)//有乘除法,有余数
                   {
                        ys = random(1,4);
                        if(value1%value2!=0)
                            jisuan1(n,value1,value2,ys,i);
                        else n++;
                   }
                   else if(value1>=value2&&n4==1&&n7==2)//有乘除法,无余数
                   {
                        ys = random(1,4);
                        if(value1%value2==0)
                            jisuan1(n,value1,value2,ys,i);
                        else n++;
                   }
                   else if(value1>=value2&&n4==2)//无乘除法
                   {
                         ys = random(1,2);
                         jisuan1(n,value1,value2,ys,i);
                   }
                   else if(value1<value2) n++;
               }
               else 
               {
                   System.out.println("输入错误!");
                   break;
               }
            }
              
        }
        public static void zhenfs(int x,int y,int n)
        {
            int value1,value2,value3,value4,ys;
            String ys1;
            
            System.out.println("请选择加减有无负数(1.有 2.无):");
            Scanner N3 = new Scanner(System.in);
            int n3=N3.nextInt();
            
            System.out.println("请选择是否有乘除法(1.有 2.无):");
            Scanner N4 = new Scanner(System.in);
            int n4=N4.nextInt();
            
            System.out.println("请选择除法有无余数(1.有 2.无):");
            Scanner N7 = new Scanner(System.in);
            int n7=N7.nextInt();
               
            for ( int i = 0; i < n; i++ ) 
            {
               value1 = random(x,y);
               value2 = random(x,y);
               value3 = random(x,y);
               value4 = random(x,y);
               if(value1<value2&&value3<value4)
               {
                   if(n3==1&&n4==1&&n7==1)//加减有负数,有乘除法,有余数
                   {
                       ys = random(1,4);
                       if(((double)value1/value2)>=((double)value3/value4)&&((double)value1/value2)%((double)value3/value4)!=0)
                           jisuan2(n,value1,value2,value3,value4,ys,i);
                       else n++;
                   }
                   else if(n3==1&&n4==1&&n7==2)//加减有负数,有乘除法,无余数
                   {
                       ys = random(1,4);
                       if(((double)value1/value2)>=((double)value3/value4)&&((double)value1/value2)%((double)value3/value4)==0)
                           jisuan2(n,value1,value2,value3,value4,ys,i);
                       else n++;
                   }
                   else if(n3==1&&n4==2)//无乘除法
                   {
                       ys = random(1,2);
                       jisuan2(n,value1,value2,value3,value4,ys,i);
                   }
                   else if(n3==2)//加减无负数
                   {
                       if(((double)value1/value2)>=((double)value3/value4)&&n4==1&&n7==1)//有乘除法,有余数
                       {
                            ys = random(1,4);
                            if(((double)value1/value2)%((double)value3/value4)!=0)
                                jisuan2(n,value1,value2,value3,value4,ys,i);
                            else n++;
                       }
                       else if(((double)value1/value2)>=((double)value3/value4)&&n4==1&&n7==2)//有乘除法,无余数
                       {
                            ys = random(1,4);
                            if(((double)value1/value2)%((double)value3/value4)==0)
                                jisuan2(n,value1,value2,value3,value4,ys,i);
                            else n++;
                       }
                       else if(((double)value1/value2)>=((double)value3/value4)&&n4==2)//无乘除法
                       {
                             ys = random(1,2);
                             jisuan2(n,value1,value2,value3,value4,ys,i);
                       }
                       else if(((double)value1/value2)<((double)value3/value4)) n++;
                   }
                   else 
                   {
                       System.out.println("输入错误!");
                       break;
                   }
                }
                else n++;
            }
        }
    }

    3.实验结果截图

  • 相关阅读:
    Fragment生命周期详解
    VS常用快捷键
    转载:C#中的泛型
    Vue 引入代码代码编辑器monacoeditor并自定义语法提示
    Monaco Editor 参数配置详解
    关于uniapp生成证书数字签名(.keystore)文件
    C#上传到FTP Server
    Linux下静态编译glib
    简单谈谈haskell
    hi,大家
  • 原文地址:https://www.cnblogs.com/qkmn/p/6537243.html
Copyright © 2020-2023  润新知