[Opgelost] Randomize Numbers From N to M - Java Task

  

2
Onderwerp starter

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

1 antwoord
2

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);
        }
    }
}
Deel: