android java随机数学除法一直显示“除以零”

rsl1atfo  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(352)

我正在尝试创建一个应用程序,通过使用开关盒生成随机操作数问题。

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从另一个名为“难度”的活动中获取整数,让用户决定他们想挑战的数学问题的难度(easy-9、medium-99、hard-999)。但不知何故,盗贼一直得到0,并兑现崩溃。
有没有办法将snumber设置在范围内,并使答案不小于1?

vfh0ocws

vfh0ocws1#

我从你的代码中看到 FNumberSNumber 永远都是积极的。可以将第二个数字钳制为永远不小于0,例如:

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

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

相关问题