Java 生成二進制加減法題目

平常算數,有益身心健康。dom

        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);
            }
        }        
相關文章
相關標籤/搜索