Advertisement
IrinaIgnatova

Randomize Words

Jul 4th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1.  
  2. package com.company;
  3.  
  4.  
  5. import java.util.*;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         String[] words = scanner.nextLine().split(" +");
  13.         Random random = new Random();
  14.         for (int i = 0; i < words.length; i++) {
  15.             int firstIndex = random.nextInt(words.length);
  16.             int secondIndex = random.nextInt(words.length);
  17.             String swapWords = words[firstIndex];
  18.             words[firstIndex] = words[secondIndex];
  19.             words[secondIndex] = swapWords;
  20.         }
  21.  
  22.         System.out.println(String.join(System.lineSeparator(), words));
  23.  
  24.  
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement