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) + "\t" + 
                        Integer.toBinaryString(a) + " + " + 
                        Integer.toBinaryString(b) + " = " + 
                        "\t\t答案:" + a + "+" + b + "=" + result);
            } else { // 减号
                a = Math.abs(random.nextInt()) % 100;
                b = Math.abs(random.nextInt()) % a;
                result = a - b;
                
                System.out.println((i + 1) + "\t" + 
                        Integer.toBinaryString(a) + " - " + 
                        Integer.toBinaryString(b) + " = " + 
                        "\t\t答案:" + a + "-" + b + "=" + result);
            }
        }        

 

posted @ 2018-05-07 18:48  李狐同学  阅读(851)  评论(0编辑  收藏  举报