settingsAccountsettings
Menusettings

Q: Randomize Numbers From N to M - Java Task

+5 votes

Write a program that takes as input two integers N and M, and randomizes the numbers between them. Note that M may be smaller than or equal to N:

randomize numbers from n to m in Java

asked in Java category by user john7
edited by user golearnweb

1 Answer

+2 votes
 
Best answer

Here is the solution for this taks, mate:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Pr_7_RandomizeNumbersFromNToM {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        int n = console.nextInt();
        int m = console.nextInt();

        int maxNumber = 0;
        int minNumber = 0;

        if (n > m) {
            maxNumber = n;
            minNumber = m;

        } else if (n < m) {
            maxNumber = m;
            minNumber = n;

        } else {
            System.out.println(n);
        }

        List<Integer> numbers = new ArrayList<>();

        for (int i = minNumber; i <= maxNumber; i++) {
            numbers.add(i);
            Collections.shuffle(numbers);
        }
        for (int item : numbers) {
            System.out.println(item);
        }
    }
}
answered by user richard8502
edited by user golearnweb
...