mardi 2 mars 2021

How to make two random generated integers divisible by each other in java?

I am a newbie and trying to write a java code in my android studio project to generate two random integers up to 100(included) and what I am actually trying to do is to check if the first random integer is bigger than the second random integer and I also want to check if the first rand.num. is divisible by the second rand.num.

So far, I am able to generate two rand. nums in OnCreate method by using Math.random():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Objects.requireNonNull(getSupportActionBar()).hide();
    setContentView(R.layout.activity_division);

    randomFirstNum = (int) (Math.random() * 100 + 1);
    randomSecondNum = (int) (Math.random() * 100 + 1);

and I am trying to write two functions.

The first function is to generate a question string depending on the rand. integer numbers. i.e.

    private void generateQuestion(int randomFirstNum, int randomSecondNum) {
        String firstNum = String.valueOf(randomFirstNum);
        String secondNum = String.valueOf(randomSecondNum);
        String question = null;
    
        if(randomFirstNum % randomSecondNum == 0 && randomFirstNum > randomSecondNum){
            question = firstNum + " ÷ " + secondNum + " = ?";
        } else if(randomSecondNum % randomFirstNum == 0 && randomSecondNum > randomFirstNum){
            question = secondNum + " ÷ " + firstNum + " = ?";
        }
    
        textViewQuestion.setText(question);
}

And the next function is to find the result of the division question. i.e.

private int findResult(int randomFirstNum, int randomSecondNum) {
    if (randomFirstNum > randomSecondNum && randomFirstNum % randomSecondNum == 0){
        return randomFirstNum / randomSecondNum;
    } else if {(randomSecondNum > randomFirstNum && randomSecondNum % randomFirstNum == 0)
        return randomSecondNum / randomFirstNum;
    }
}

I know the code is not right and there are missing parts, I am just stucked here. But I hope I was able to explain my issue a bit. I want to be able to generate a question where the first rand.integer will be either bigger than the second rand.integer or equal to the second rand.integer on the secreen ( i.e "20 ÷ 2 = ?" or "21 ÷ 7 = ?" or 11 ÷ 11 = ? - There might be a chance that two rand. integers are gonna be the same numbers :)

I am just stucked at the point of what to do if the both integers are odds (i.e. "17 ÷ 11 = ?" I don't want to end up with these kind of questions :)

I would appreciate a lot if someone can shad some light on my question and hopefully provide a clear answer for me! :) Thank you so much for sticking around and spend your time to read my question.




Aucun commentaire:

Enregistrer un commentaire