• 软件工程个人作业01


        

         思路:

                       定义俩String类型数组,用数组来存储每道题,然后利用Random和Scanner,完成有效的四则运算即可。

      源代码:

    //2017.3.6   严鹏     20153314
    package dome;
    import java.util.Random;
    import java.util.Scanner;
    public class Add1 {
        public static void main(String[] args)
        {
            Random random=new Random();
            Scanner sr=new Scanner(System.in);
            System.out.println("请输入作业数量:");
            int n=sr.nextInt();
            System.out.println("1.整数运算      2.真分数运算");
            int n1=sr.nextInt();
            String[] s1 = new String[n];
            String[] s2 = new String[n];
            s1=Zsjs(n);
            s2=Zfsjs(n);
            if(n1==1)        
            {
                for(int i = 0;i < n;i++) {
                    String[] t = s1[i].split("=");  //分割字符串,用来取值判等
                    System.out.print((i + 1) + " : " + t[0] + " = " + "( )");
                    String r = sr.next();
                    if(r.equals(t[1])){
                        System.out.println("回答正确!");
                    }else{
                        System.out.println("回答错误! "+t[1]);
                    }
                }
            }
            else
            {    
               for(int i = 0;i < n;i++) {
                String[] t = s2[i].split("=");
                System.out.print((i + 1) + " : " + t[0] + "=" + "( )");
                String r = sr.next();
                if(r.equals(t[1])){
                    System.out.println("回答正确!");
                }else{
                    System.out.println("回答错误!"+t[1]);
                }
                   }
            }
                
            /*int a=100,b=4;
            int n=0;
            int[] s1=new int[100];
            int[] s2=new int[100];
            while(n!=30){
            
            int a1=random.nextInt(a)+1;
            int a2=random.nextInt(a)+1;
            int b1=random.nextInt(b)+1;
            s1[n]=a1;s2[n]=a2;
            {
            
                 if(b1==1)
                     {System.out.println(a1+"+"+a2+"=");n++;}
                     else if(b1==2)
                     { System.out.println(a1+"-"+a2+"=");n++;}
                     else if(b1==3)
                     { System.out.println(a1+"*"+a2+"=");n++;}
                     else
                     { System.out.println(a1+"/"+a2+"=");n++;}
                  
                
            }
            }*/
        }
        //真分数计算式
    
            public static String[] Zfsjs(int n) {
                int x,y,z,a,b,i = 0;
                String[] ss = new String[n];
                String t = "";
                Random sr = new Random();
                
                while(i < n) {
                    a = sr.nextInt(100);
                    b = sr.nextInt(100);
                    
                    
                    
                    if(a != 0 && b != 0){
                        x = sr.nextInt(a);
                        y = sr.nextInt(b);
                        z = sr.nextInt(4);
                        t = "";
                        if(y != 0){
                            if(z == 0) {
                                if((x * b + y * a) < (a * b))
                                    t = "" + Yj(x, a) + " + " + Yj(y, b) + "=" + Yj((x * b + y * a), (a * b));
                            
                            }else if(z == 1) {
                                if((x * b - y * a) >= 0 && (x * b - y * a) < (a * b))
                                    t = "" + Yj(x, a) + " - " +  Yj(y, b) + "=" +  Yj((x * b - y * a), (a * b));
                            
                            }else if(z == 2) {
                                if((x * y) < (a * b))
                                    t = "" +  Yj(x, a) + " * " +  Yj(y, b) + "=" +  Yj((x * y),(a * b));
                            
                            }else{
                                if((y / b) != 0){
                                    if((x * b) < (a * y))
                                        t = "" + Yj(x, a) + " / " +  Yj(y, b) + "=" +  Yj((x * b),(a * y));
                                }
                            }
                            if((!t.equals("")) && check(ss,t,i)){
                                ss[i++] = t;
                            }
                        }
                    }
                }
                return ss;
            }
            
            //生成整数计算式
    
            public static String[] Zsjs(int n) {
                int a1,a2,z,i = 0;
                String[] ss = new String[n];
                String t = "";
                Random rd = new Random();
                while(i < n) {
                    a1 = rd.nextInt(100);
                    a2 = rd.nextInt(100);
                    z = rd.nextInt(4);
                    
                        if(z == 0) {
                            t = "" + a1 + " + " + a2  + "=" + (a1 + a2);
                            
                        }else if(z == 1 && a1 >= a2) {
                            t = "" + a1 + " - " + a2  + "=" + (a1 - a2);
                            
                        }else if(z == 2) {
                            t = "" + a1 + " * " + a2  + "=" + (a1 * a2);
                            
                        }else{
                            if(a2 != 0){
                                if(a1 % a2 == 0)
                                    t = "" + a1 + " / " + a2  + "=" + (a1 / a2);
                            }
                        }
                        
                        if(check(ss,t,i)){
                            ss[i++] = t;
                        }
                    
                }
                return ss;
            }
            
            //约简分子分母,如果分子为0则返回0
            public static String Yj(int a,int b){
                
                int d;
                int ac = a,bc = b;
                if(a == 0)
                    return "0";
                while(a != b - a){
                    d = b - a;
                    if(a > d){
                        b = a;
                        a = d;
                    }else{
                        b = d;
                    }
                }
                d = a;
                int z = ac / d;
                int m = bc / d;
                return "" + z + "/" + m;
            }
            
            //检测是否重复
    
            public static boolean check(String[] s,String m,int n) {
                for(int i = 0;i < n;i++) {
                    if(s[i].equals(m)){
                        return false;
                    }
                }
                return true;
            }
        
    }

     运行结果截图:

     

  • 相关阅读:
    wamp集成环境安装后无法启动的问题
    jquery点击内层的click事件时会触发外层的click事件
    js 控制文本框只能输入数字
    第七届飞思卡尔智能车比赛的赛道边缘提取第一篇博客
    多级菜单,多级下拉列表解决方案(收藏) 西安
    ASP.NET 从Excel文件导入数据到数据库(笔记) 西安
    动态构造地址栏参数 西安
    我觉得我应该要回来了 西安
    SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志(转) 西安
    Web.Config 分析 西安
  • 原文地址:https://www.cnblogs.com/ypbk/p/6511034.html
Copyright © 2020-2023  润新知