Android Java 随机数学除法一直显示 "divide by zero"

Android Java Random math division keep showing "divide by zero"

我正在尝试创建一个使用 switch case 生成随机操作数问题的应用程序。

        MaxDigit = Difficulty.Level;
    Random RandomNumber = new Random();

    //Set the First and Second Number to Random Number
    FNumber = RandomNumber.nextInt(MaxDigit);
    SNumber = RandomNumber.nextInt(MaxDigit);


    Random OperandsChoise = new Random();
    int Operands = OperandsChoise.nextInt(4);

    if ((FNumber / SNumber) >= 1) {
        switch (Operands){

            case 0 :
                this.Answer = this.FNumber + this.SNumber;
                this.QTyoe = FNumber + "+" + SNumber + "=";
                break;

            case 1 :
                this.Answer = this.FNumber - this.SNumber;

                this.QTyoe = FNumber + "-" + SNumber + "=";
                break;

            case 2 :
                this.Answer = this.FNumber * this.SNumber;

                this.QTyoe = FNumber + "*" + SNumber + "=";
                break;

            case 3 :
                this.Answer = (this.FNumber +2) / (this.SNumber +1);

                this.QTyoe = FNumber + "/" + SNumber + "=";
                break;
        }
    }

MaxDigit 正在从另一个名为“难度”的 activity 获取整数,让用户决定他们想要挑战的数学问题的难度(Easy-9、Medium-99、Hard-999)。但不知何故,SNumber 一直为 0 和兑现崩溃。

有没有什么办法可以把SNumber设置在范围内,让Answer不小于1?

我从你的代码中看到 FNumberSNumber 总是正数。您可以限制第二个数字永远不会小于 0,例如:

SNumber = RandomNumber.nextInt(MaxDigit - 1) + 1;

上面的代码片段将确保 SNumber 永远不会大于 MaxDigit 而始终大于或等于 1。