zoukankan      html  css  js  c++  java
  • 软件工程个人作业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;
            }
        
    }

     运行结果截图:

     

  • 相关阅读:
    数组指针的一个易错点
    jQuery on()方法
    php 前一天或后一天的日期
    用jQuery监听浏览器窗口的变化
    jquery获取json对象中的key小技巧
    JQuery操作元素的属性与样式及位置
    用JQuery操作元素的style属性
    如何删除jsPlumb连接
    jsPlumb.jsAPI阅读笔记(官方文档翻译)
    Jquery empty() remove() detach() 方法的区别
  • 原文地址:https://www.cnblogs.com/ypbk/p/6511034.html
Copyright © 2011-2022 走看看