jeudi 8 juillet 2021

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

I'm trying to create an app that will generate random operands questions by using a 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;
        }
    }

The MaxDigit is getting integers from another activity called "Difficulty" to let users decide how hard the math question they want to challenge (Easy-single digit, Medium-double digit, Hard-Triple Digit). But somehow the SNumber keeps getting 0 and cashing crash.

Is there any way to set SNumber within the range and make the Answer will not less than 1?




Aucun commentaire:

Enregistrer un commentaire