Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

290570

24.05.16 본문

java_spring (2024.05 ~ 2024.10)

24.05.16

qordpsem 2024. 5. 16. 10:04

#배열 정렬하기

- 값을 순서대로 오도록 배치하는 것

 

*오름차순 정렬 : 값이 작은 것부터 순서대로 오도록 하는 것

*내림차순 정렬 : 값이 큰 것부터 순서대로 오도록 하는 것

public class D02SortArray {
	public static void main(String[] args) {
		int[] a = {17,16,15,7,9,11};
		
		//오름차순 정렬
		for(int i=0; i<a.length; i++) {
			for(int j=i+1; j<a.length; j++) {
				if(a[j]<a[i]) {
					int temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}
		}
		System.out.println("ㅡㅡ이번주 추첨번호ㅡㅡ");
		for(int i=0; i<a.length; i++) {
			System.out.printf("%5d", a[i]);
		}
	}
}

 

 

 

#메소드 호출

*값에 의한 호출

- 메소드 호출 시 기본자료형이 전달되는 것

- 메소드 안에서 매개변수로 전달된 값이 변경되더라도 호출한 쪽에서는 적용되지 않음

 

*참조에 의한 호출

- 메소드 호출 시 참조자료형이 전달되는 것

- 메소드 안에서 매개변수로 전달된 변수의 내용이 변경 될때는 호출한 쪽에서도 적용됨

 

 

public class D03Method {
	//정수형 변수 하나를 매개변수로 전달받아 1 증가하여 출력
	public static void add(int n) {
	n = n+1;
	System.out.println("n: "+n);
	}
	public static void main(String[] args) {
		int a;
		a = 5;
		add(a);
		System.out.println("a: "+a);
	}
}

 

호출할때 전달해준 a 는 변동이 없음 (값에 의한 호출은 메소드안에서 변경되어도 호출한 쪽은 변동X)

 

public class D04Method {
	//정수형 배열을 매개변수로 전달받아 배열의 요소를 모두 1씩 증가하는 메소드
	public static void add(int[] arr) {
	for(int i=0; i<arr.length; i++) {
		arr[i] = arr[i] + 1;
	}
	for(int i=0; i<arr.length; i++) {
		System.out.printf("%5d",arr[i]);
	}
	System.out.println();
}
	public static void main(String[] args) {
		int[] data= {1,2,3,4,5};
		add(data); //참조에 의한 호출
		for(int i =0; i<data.length; i++){
			System.out.printf("%5d",data[i]);
		}
	}
}

 

메소드 호출한 곳에도 적용됨

 

 

 


import java.util.Scanner;

public class D05StudentTest {
	//5명 학생의 국 영 수 입력받아  총점, 평균 구한 후 성적순 정렬
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		
		String[] name = new String[5];
		int[] kor = new int[5];
		int[] eng = new int[5];
		int[] math = new int[5];
		int[] tot = new int[5];
		double[] avg = new double[5];

		for(int i=0 ; i<name.length; i++) {
			System.out.printf("%d번째 학생 이름 :", i+1);
			name[i] = sc.next();
			System.out.printf("%d번째 학생 국어 점수 :", i+1);
			kor[i] = sc.nextInt();
			System.out.printf("%d번째 학생 영어 점수 :", i+1);
			eng[i] = sc.nextInt();
			System.out.printf("%d번째 학생 수학 점수 :", i+1);
			math[i] = sc.nextInt();
			tot[i] = kor[i]+eng[i]+math[i];
			avg[i] = tot[i] / 3.0;
		}
		
		for(int i=0; i<tot.length; i++) {
			for(int j = i+1; j<tot.length; j++) {
				if(tot[j]>tot[i]) { 
					String tempName = name[i]; 
					name[i] = name[j]; 
					name[j] = tempName; 
					int temp = kor[i];
					kor[i] = kor[j];
					kor[j] = temp;
					
					temp = eng[i];
					eng[i] = eng[j];
					eng[j] = temp;
					
					temp = math[i];
					math[i] = math[j];
					math[j] = temp;
					
					temp = tot[i];
					tot[i] = tot[j];
					tot[j] = temp;
					
					double temp_avg = avg[i];
					avg[i] = avg[j];
					avg[j] = temp_avg;
				} //end if
			} // end for j
		} // end for i
		for(int i =0; i<name.length; i++) {
			System.out.printf("이름 : %s\n국어 : %d\n영어 : %d\n수학 : %d\n총점 : %d\n평균 : %.2f\n\n",name[i],kor[i],eng[i],math[i],tot[i],avg[i]);
			}
	}
}

- 위 코드는 성적순으로 정렬하는 코드

 

- 총점을 비교하여 비교하는 학생의 총점이 i번째 학생의 총점보다 크다면 이름, 국어, 영어,수학,총점, 평균 값 각각 바꿈

 

- 위와 같이 기본자료형만으로 프로그래밍 할때 불가능한건 아니지만 비효율적

 

- 이럴때 한명의 학생 정보 (이름,국어,영어,수학,총점,평균)을 하나의 세트로 하는 새로운 자료형을 만들면 더 간결하게 표현 가능

 

아래 코드와 같이 수정 가능

 

import java.util.Scanner;

class Student{
	String name;
	int kor;
	int eng;
	int math;
	int tot;
	double avg;
	};
	
	public class D09{
		public static void main(String[] args) {
			
			Student[] data = new Student[5]; //Student 의 변수 5개를 만듬 (객체생성아님)
			Scanner sc =new Scanner(System.in);
			for (int i=0; i<data.length ; i++) {
				data[i] = new Student(); //객체 생성!!
				System.out.printf("%d번째 학생 이름 : ", i+1);			
				data[i].name = sc.next();
				System.out.print("국어 성적 : ");
				data[i].kor = sc.nextInt();
				System.out.print("영어 성적 : ");
				data[i].eng = sc.nextInt();
				System.out.print("수학 성적 : ");
				data[i].math = sc.nextInt();
	
				data[i].tot = data[i].kor + data[i].eng + data[i].math;
				data[i].avg = data[i].tot / 3.0;
			}

			for(int i = 0; i<data.length; i++) {
				for(int j = i+1; j<data.length; j++) {
					if(data[j].tot > data[i].tot) {
						Student temp = data[i];
						data[i] = data[j];
						data[j] = temp;
					}
				}
			}
			System.out.println("-----성적순으로 정렬 결과-----");
			System.out.println("이름\t국어\t영어\t수학\t총점\t평균");
			for(int i =0; i<data.length; i++) {
				System.out.printf("%s\t%d\t%d\t%d\t%d\t%.2f\n",
						data[i].name,data[i].kor,data[i].eng,
						data[i].math,data[i].tot,data[i].avg);
			}
		}		
	}

'java_spring (2024.05 ~ 2024.10)' 카테고리의 다른 글

24.05.17  (0) 2024.05.17
05.16  (0) 2024.05.16
24.05.15  (0) 2024.05.15
24.05.10  (0) 2024.05.10
05.09 부동소수점 오류  (0) 2024.05.09