1~10까지 난수를 100개 발생시켜 한 줄에 10개씩 출력하고 마지막에 각 숫자별 발생 횟수를 출력하는 프로그램을 작성

	int num; // 난수를 발생시키기 위한 변수
	int [] count = new int[10]; // 1~10까지 난수의 발생결과의 갯수를 저장하는 곳

	System.out.println("발생된 난수(1~10)...");
	for(int i=1; i<=100; i++) {
		num = (int)(Math.random() * 10) + 1;
   		// 0.0 ~ 0.9 사이의 실수를 발생시킨다 따라서 1~10까지의 수로 바꿔준다.
   		count[num-1]++; // 배열의 첫번째 방의 주소는 0부터 시작임.
    
    	System.out.printf("%3d", num);
    	if(i%10 == 0) { //한줄에 10개씩 출력
    		System.out.println();
        }
	}
System.out.println("\n숫자별 발생횟수 : ");
for(int i=0; i<count.length; i++) {
	System.out.println((i+1)+ "의 발생횟수 :  " + count[i]);
    }

+ Recent posts