본문 바로가기

JAVA/기초 프로그래밍

함수//shuffle

		/*
		(1) shuffle메소드를 작성하시오.
		*/  

		int[] original = {1,2,3,4,5,6,7,8,9};
		System.out.println(java.util.Arrays.toString(original));
		shuffle(original);
		System.out.println(java.util.Arrays.toString(original));
		}
        
        
        
        
		static void shuffle(int [] original) {//스왑사용
			
			int temp;
			for (int i = 0; i < 100; i++) {//random을 수행하기 위한 loop문
				int r1 = (int)(Math.random()*original.length);//0~8
				int r2 = (int)(Math.random()*original.length);//0~8
				
				temp = original[r1];
				original[r1] = original[r2];
				original[r2] = temp;
			}
			
	}