준코딩

BubbleSort 버블정렬 본문

프로그래밍/자바

BubbleSort 버블정렬

Ljunhyeob - App Dev 2018. 12. 10. 14:33

1.int형 범위 숫자만 입력가능

2. 공백입력 불가

3. 10개의 숫자 입력




import java.util.Scanner;


public class BubbleSort {


public static void main(String[] args) {


int[] arr = new int[10];


Scanner scan = new Scanner(System.in);

System.out.println("공백입력불가");

for (int i = 0; i < arr.length; i++) {

while (true) {

System.out.print((i + 1) + "번째 숫자 입력: ");

try {

arr[i] = Integer.parseInt(scan.nextLine());

} catch (NumberFormatException e) {

System.out.println("오류가 발생했습니다.");

continue;

}

catch(Exception e){

System.out.println("예기치 못한 오류 발생");

}

break;

}

}

System.out.print("정렬전 데이터:");


for (int i = 0; i < arr.length; i++) {


System.out.print(" " + arr[i]);


}


System.out.println();


System.out.print("정렬후 데이터:");


for (int i = 0; i < arr.length; i++) {


for (int j = 0; j < arr.length - 1; j++) {


if (arr[j] > arr[j + 1]) {


int temp = arr[j];


arr[j] = arr[j + 1];


arr[j + 1] = temp;


}


}


}


for (int i = 0; i < arr.length; i++) {


System.out.print(" " + arr[i]);


}


}

}



'프로그래밍 > 자바' 카테고리의 다른 글

최대값 구하기  (0) 2019.01.09
버블정렬코드 (스택, 큐 , 정렬)  (0) 2018.12.11
예외처리  (0) 2018.11.29
객체지향 프로그래밍-2  (0) 2018.11.29
객체지향 프로그래밍  (0) 2018.11.27
Comments