반응형

1. 배열의 랜덤 숫자 넣고 출력하기 ver1

import java.util.Random;
public class Hello{
public static void main(String[] args) {
	Random rand = new Random();
	final int size = 6;
	int[] array = new int[size];
	for(int i=0; i<10000; i++) {
		array [(int)(Math.random() * 6)]++;
		}
	for(int i=0; i<size; i++) {
		System.out.printf("%3d %3d \n", i+1, array[i]);
	}
	}
}
  1 1628 
  2 1635 
  3 1714 
  4 1670 
  5 1622 
  6 1731 

ver2

import java.util.Random;
public class Hello{
public static void main(String[] args) {
	final int size = 10;
	Random rand = new Random();
	int[] array = new int[size];
	for(int i=0; i<10; i++) {
		array[i] = rand.nextInt(10);
		System.out.print(array[i] + " ");
		}
	}
}
/*
6 4 9 1 2 1 7 2 2 4 
*/

 

반응형

'JAVA > 2020 프로그램' 카테고리의 다른 글

[JAVA] GUI 프로그래밍  (0) 2020.01.12
[JAVA] 로또 번호 추출 프로그램  (0) 2020.01.05
[JAVA] 숫자 출현횟수  (0) 2019.12.15
[JAVA] 가위바위보 게임  (0) 2019.12.08
[JAVA] 별찍기  (0) 2019.12.08

+ Recent posts