Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that enters in integer n and prints the numbers 1, 2, …, n in random order.
- * You might need to use arrays */
- import java.util.Random;
- import java.util.Scanner;
- public class _12_RandomizeTheNumbers1toN {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a whole positive number, bigger from 1, for N: ");
- int numN = scan.nextInt();
- scan.close();
- if (numN > 1) {
- boolean[] randomNums = new boolean[numN + 1];
- randomNums[0] = true;
- Random generator = new Random();
- for (int i = 1; i < randomNums.length; i++) {
- int random = 1 + generator.nextInt(numN);
- if (randomNums[random]) {
- i--;
- } else {
- randomNums[random] = true;
- System.out.print(random);
- if (i < randomNums.length - 1) {
- System.out.print(" ");
- }
- }
- }
- } else {
- System.out.println("Error! - Invalid Input number!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement