본문 바로가기
Back/JAVA

[문제] 배열을 이용하여 정수를 내림차순으로 정렬하기

by 시월해 2021. 3. 4.
public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수형 배열의 크기 : ");
		
		int[] score = new int[sc.nextInt()];
		
		for(int i=0; i<score.length; i++) {
			
			System.out.print((i+1)+"번째 정수 입력 : ");
			
			score[i] = sc.nextInt();
		}
		
		
		// 내림차순으로 정렬
		int temp = 0;    // 임시적으로 저장될 변수
		
		for(int i=0; i<score.length; i++) {
			for(int j=i+1; j<score.length; j++) {
				if(score[j] > score[i]) {
					temp = score[i];
					score[i] = score[j];
					score[j] = temp;
				}
			}
		}
		
		// 내림차순으로 정렬된 데이터를 화면에 출력해 보자.
		for(int i=0; i<score.length; i++) {
			System.out.print(score[i] + "\t");
		}
		
		System.out.println();
		
		sc.close();
        
 }

큰 수부터 정렬된 것을 볼 수 있다.

 

'Back > JAVA' 카테고리의 다른 글

[문제] 배열을 사용한 성적 처리 프로그램  (0) 2021.03.04
다차원 배열과 가변 배열  (0) 2021.03.04
배열 공유  (0) 2021.03.04
배열(Array)  (0) 2021.03.03
[문제] 카페 음료 계산 프로그램  (0) 2021.03.03