Advertisement
veronikaaa86

01. Randomize Words

Jun 22nd, 2023
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. package objectsAndClasses;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class P01RandomizeWords {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] wordsArr = scanner.nextLine().split(" ");
  11.  
  12.         Random randomObj = new Random();
  13.  
  14.         for (int i = 0; i < wordsArr.length; i++) {
  15.             int rndIndexX = randomObj.nextInt(wordsArr.length);
  16.             int rndIndexY = randomObj.nextInt(wordsArr.length);
  17.  
  18.             String oldWord = wordsArr[rndIndexX];
  19.             wordsArr[rndIndexX] = wordsArr[rndIndexY];
  20.             wordsArr[rndIndexY] = oldWord;
  21.         }
  22.  
  23.         System.out.println(String.join(System.lineSeparator(), wordsArr));
  24.     }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement