Created
November 2, 2016 02:06
-
-
Save tae0y/41949b81607a843329cc72579926ab76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
import java.util.Random; | |
import java.util.Scanner; | |
/** | |
* Created by belobster on 16. 11. 2. | |
*/ | |
public class Randomize { | |
public static void main(String[] args){ | |
Scanner sc = new Scanner(System.in); | |
System.out.print("항목 개수, 선택 개수 : "); | |
int n = sc.nextInt(); | |
int s = sc.nextInt(); | |
String[] arr = new String[n]; | |
System.out.println(); | |
System.out.println("항목을 입력하세요."); | |
for(int i=0; i<n; i++){ | |
arr[i] = sc.next(); | |
} | |
System.out.println(); | |
System.out.print("[ "); | |
Random r = new Random(); | |
for(int j=0; j<s; j++){ | |
int t = r.nextInt(n-1-j); | |
System.out.print(arr[t]+", "); | |
if(t!=n-1){ | |
String swap = arr[t]; | |
arr[t] = arr[n-1]; | |
arr[n-1] = arr[t]; | |
} | |
} | |
System.out.print("]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment