• 随机生成300道四则运算


    package work;
    
    public class mathwork {
        public static void subject(Object obj) {
            System.out.println(obj);
            }
            public static void main(String[] args) {
            subject("四则运算练习题:");
            int count = 0;
            for (int i = 0; i < 300; i++) {
            int d1 = (int) ((Math.random()) * 100);
            int d2 = (int) ((Math.random()) * 100);
            int k = (int) ((Math.random()) * 100);
            int k2 = (int) ((Math.random()) * 100);
            int k3 = (int) ((Math.random()) * 100);
            int k4 = (int) ((Math.random()) * 100);
            int x = 1;
            if (0 <= k && k < 12)
            x = 1;
            else if (12 <= k && k < 24)
            x = 2;
            else if (24 <= k && k < 36)
            x = 3;
            else if (36 <= k && k < 48)
            x = 4;
            else if (48 <= k && k < 60)
            x = 5;
            else if (60 <= k && k < 72)
            x = 6;
            else if (72 <= k && k < 84)
            x = 7;
            else
            x = 8;
            switch (x) {
            case 1:
            subject(d1 + "+" + d2 + "=");
            break;
            case 2:
            jiafa(d1, d2);
            break;
            case 3:
            subject(d1 + "*" + d2 + "=");
            break;
            case 4:
            chufa(d1, d2);
            break;
            case 5:
            subject(zhenfenshu(k, k2) + "+" + zhenfenshu(k3, k4) + "=");
            break;
            case 6:
            subject(zhenfenshu(k, k2) + "-" + zhenfenshu(k3, k4) + "=");
            break;
            case 7:
            subject(zhenfenshu(k, k2) + "*" + zhenfenshu(k3, k4) + "=");
            break;
            case 8:
            subject(zhenfenshu(k, k2) + "/" + zhenfenshu(k3, k4) + "=");
            break;
            default:
            subject("stop,stop,stop");
            break;
            }
            count++;
            }
            subject("以上共有" + count + "四则运算练习题!");
            }
            public static void jiafa(int a, int b) {
            if (a > b)
            subject(a + "-" + b + "=");
            else {
            subject(b + "-" + a + "=");
            }
            }
            public static void chufa(int a, int b) {
            if (b > 0)
            subject(a + "/" + b + "=");
            else {
            b = b + 10;
            subject(a + "/" + b + "=");
            }
            }
            public static String zhenfenshu(int a, int b) {
            if (a <= b)
            return a + "/" + b;
            else
            return b + "/" + a;
            }
            }

    先写出随机生成数字的代码,然后编写逻辑代码进行加减乘除的运算。

    这是运行之后的结果。

  • 相关阅读:
    ASP.NET 自制时间控件
    ORACLE 函数汇总之单记录函数
    Servers IIS 重启命令
    ASP.NET 两个Dropdownlist控件联动
    ASP.NET datagridview控件表头定义
    python Image 安装
    ssh 不需要密码的链接
    [Redis] redis 相关的博客
    [emacs] python代码折叠
    linux python 链接 oracle
  • 原文地址:https://www.cnblogs.com/u1118733/p/6528032.html
Copyright © 2020-2023  润新知