Java 生成二进制加减法题目

日常算数,有益身心健康。

        int a;
        int b;
        int result;
        int symbol;
        int count = 50;
        Random random = new Random();
        for(int i = 0; i < count;i++) {
            symbol = Math.abs(random.nextInt()) % 2;
            
            if (symbol == 0){ // 加号
                a = Math.abs(random.nextInt()) % 100;
                b = Math.abs(random.nextInt()) % 100;
                result = a + b;
                
                System.out.println((i + 1) + "	" + 
                        Integer.toBinaryString(a) + " + " + 
                        Integer.toBinaryString(b) + " = " + 
                        "		答案:" + a + "+" + b + "=" + result);
            } else { // 减号
                a = Math.abs(random.nextInt()) % 100;
                b = Math.abs(random.nextInt()) % a;
                result = a - b;
                
                System.out.println((i + 1) + "	" + 
                        Integer.toBinaryString(a) + " - " + 
                        Integer.toBinaryString(b) + " = " + 
                        "		答案:" + a + "-" + b + "=" + result);
            }
        }        
原文地址:https://www.cnblogs.com/foxlee1024/p/9004363.html