简体   繁体   中英

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. ie

    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. ie

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 ( ie "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 (ie "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.

First number can be generated without any constraints, but to cater first number must be divisible by second number and first number >= second number requirement, you have a constraint that second number must be the divisor of first number as it will fulfil both requirements.

So you can generate first number from 1 to 100 and whatever that number is, find out its divisors and pick a random divisor from the divisors list.

private static void generateQuestion() {
       int first = (int) (Math.random() * 100 + 1);
       List<Integer> firstNumDivisors = getDivisors(first);
       
       int divisorsRandIndex = (int) (Math.random() * firstNumDivisors.size());
       int second = firstNumDivisors.get(divisorsRandIndex);

    System.out.println(first + " " + second);
    // Create your question from first and second numbers here
}

private static List<Integer> getDivisors(int number) {
    List<Integer> divisors = new ArrayList<>();

    for (int i=1;i<=number;i++) 
        if (number%i==0) 
            divisors.add(i);
            
    return divisors;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM