落花为谁 2023-05-08 20:26 采纳率: 66.7%
浏览 37
已结题

Java加减法两步运算

import java.util.Random;
class MathQuestion {
    private int num1;
    private int num2;
    private int num3;
    private String operator;

    private int answer;
    private boolean isCorrect = true;

    public MathQuestion(int grade, int type) {
        Random random = new Random();
        if (grade == 1) {
            if (type == 1)
            { // 10以内加减法:1步计算
                this.num1 = random.nextInt(10);
                this.num2 = random.nextInt(num1 + 1);
                this.operator = random.nextInt(2) == 0 ? "+" : "-"; //使用 random.nextInt(2) 生成一个 0 或 1 的随机数,然后根据随机数来决定是加法还是减法。
            }
            else if (type == 2)
            { // 10以内加减法:2步计算  如:8-2-3,6-2+3
                this.num1 = random.nextInt(10); // 第一步运算的第一个数
                this.num2 = random.nextInt(num1 + 1); // 第一步运算的第二个数
                this.num3 = random.nextInt(num2 + 1); // 第二步运算的第二个数
                this.operator = "-";
            }
            else if (type == 3)
            { // 10-20以内的加减法:1步计算和2步计算
                if (random.nextBoolean())
                { // 1步计算
                    this.num1 = random.nextInt(11) + 10;
                    this.num2 = random.nextInt(10);
                }
                else
                { // 2步计算
                    this.num1 = random.nextInt(11) + 10;
                    this.num2 = random.nextInt(num1-9) + 10;
                    int num3 = random.nextInt(num1-num2+1) + num2;
                    this.num2 = num3;
                    this.operator = "-";
                }
            }
            else if (type == 4)
            { // 综合训练
                int t = random.nextInt(3) + 1;
                this.isCorrect = false; // 默认为错误答案,便于后面判断
                if (t == 1)
                { // 10以内加减法:1步计算
                    this.num1 = random.nextInt(10);
                    this.num2 = random.nextInt(num1+1);
                    this.operator = random.nextBoolean() ? "+" : "-";
                    this.answer = this.operator.equals("+") ? num1 + num2 : num1 - num2;
                }
                else if (t == 2)
                { // 10以内加减法:2步计算
                    this.num1 = random.nextInt(10);
                    this.num2 = random.nextInt(num1+1);
                    int num3 = random.nextInt(num2+1);
                    this.num1 = this.num1 + num2;
                    this.num2 = num3;
                    this.operator = random.nextBoolean() ? "+" : "-";
                    this.answer = this.operator.equals("+") ? num1 + num2 + num3 : num1 - num2 - num3;
                }
                else if (t == 3)
                { // 10-20以内的加减法:1步计算和2步计算
                    if (random.nextBoolean()) { // 1步计算
                        this.num1 = random.nextInt(11) + 10;
                        this.num2 = random.nextInt(10);
                        this.operator = random.nextBoolean() ? "+" : "-";
                        this.answer = this.operator.equals("+") ? num1 + num2 : num1 - num2;
                    }
                    else
                    { // 2步计算
                        this.num1 = random.nextInt(11) + 10;
                        this.num2 = random.nextInt(num1-9) + 10;
                        int num3 = random.nextInt(num1-num2+1) + num2;
                        this.num2 = num3;
                        this.operator = random.nextBoolean() ? "+" : "-";
                        this.answer = this.operator.equals("+") ? num1 + num2 + num3 : num1 - num2 - num3;
                    }
                }
            }
        }
}

有懂Java的uu吗,这个2步计算该怎么弄啊,试了发现还是只显示一步计算。可以帮忙看看这段代码并修改吗?

  • 写回答

1条回答 默认 最新

  • 喝茶品人生 2023-05-08 22:39
    关注

    二步计算修改:

    if (type == 2) {
        this.num1 = random.nextInt(10); // 第一步运算的第一个数
        this.num2 = random.nextInt(num1 + 1); // 第一步运算的第二个数
        int result1 = num1 - num2; // 第一步运算的结果
        this.num3 = random.nextInt(num2 + 1); // 第二步运算的第二个数
        this.answer = result1 - num3; // 总的运算结果
        this.operator = "-";
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 创建了问题 5月8日

悬赏问题

  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥15 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入
  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?