samedi 26 septembre 2020

Finding the multiple in while loop

The return type is int and the input parameters are also int.

I am generating random integers in the while loop and I am checking if that int is the multiple of the input parameter int and the return the number of tries it took to get that matched one.

And I want to do it in a while loop and not in a for loop.

public int findMul(int v){
    double random = Math.random();
    int numoftries = 0;

    while(v % random == 0){
        numoftries++;
    }
    return numoftries;
}

For some reason when I call it in the main method then execute it, it always shows the number 0 no matter what number and I don't know how to fix.




Aucun commentaire:

Enregistrer un commentaire