'Math.random()'에 해당되는 글 1건

  1. 2011.06.30 java 주사위, 확률

import java.util.Scanner ;

public class dice {
	static int dice, roll;
	static int count[] = new int[6];
	static float pro[] = new float[6];
	public static void main(String[] args) {
		System.out.print("주사위 회전 횟수 : ");
		Scanner scan = new Scanner(System.in);
		roll = scan.nextInt();
		
		for(int i=0;i<roll;i++){
			dice = (int)(Math.random()*10);
			while(dice > 5){
				dice = (int)(Math.random()*10);
			}
			for(int j=0;j<6;j++){
				if(dice == j){
					count[j]++;
				}
			}
			dice ++;
			//System.out.println(dice);
		}
		System.out.println("\n\t" + roll + "번 중" + "\n");
		for(int i=0;i<6;i++){
			pro[i] = (float)count[i] / roll * 100;
			pro[i] = Math.round(pro[i]*100);
			pro[i] = pro[i]/100;
			System.out.println(i+1 + "번 주사위 : "+count[i] +"번, 확률 : "+ pro[i] +"%");
		}
	}

}
Posted by lemonT