본문 바로가기
JAVA

221226_배열

by 경 훈 2022. 12. 26.

변수 : 값을 저장소 1변수는 1개의 값만 지정

값이 10개 있으면 변수도 10개 필요

 

배열 : array

동일한 자료를 많이 저장 구조 (for 문과 같이 사용할때가 많다)

int [] a = new int[10]   ..  10 -> 첨자 기억장소 갯수

a[0] a[1] a[2] ... a[9]

 

package day16_평가피드백;

import java.util.InputMismatchException;
import java.util.Scanner;

public class MyinfoMain {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		MyinfoDAO dao = new MyinfoDAO();
		while(true) {
			try {
				System.out.println("[1]입력");
				System.out.println("[2]출력");
				System.out.println("[0]종료");
				System.out.print("[작업번호]입력");
//				int n = sc.nextInt();
				String nn= sc.next();
				int n = Integer.parseInt(nn);
				switch (n) {
				case 0:System.exit(0);break;
				case 1:dao.insert();break;
				case 2:dao.out();break;
				}
			}
			catch(InputMismatchException e) {
				System.out.println("문자를 입력하여 오류발생");
			}
			catch(NumberFormatException e) {
				System.out.println("문자를 입력하여 오류발생");
			}
		}
	}
}

 

package day16_평가피드백;

import java.util.Scanner;

class KKH extends Exception{ //KKH라는 사용자 예외처리 클래스 생성
	public KKH(String message) {
		super(message);
	}
}

public class UserExceptionTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true) {
			try {
				System.out.print("수를 입력하세요");
				int a = sc.nextInt();
				if(a % 2 == 1) {
					throw new KKH("홀수는 안됩니다!");
				}
				else {
					System.out.println(a);
				}
			}
			catch(KKH e) {
				System.out.println(e.getMessage());
			}
		}
	}
}

package day16_평가피드백;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ArrayTest {
	public static void main(String[] args) throws FileNotFoundException {
		int [] a = new int[5]; // 고정방식
		FileReader fr = new FileReader("c:/data/1226.txt");
		BufferedReader br = new BufferedReader(fr);
		int i=-1;
		while(true) {
			try {
				String str=br.readLine();
				if(str==null)break;
				i++; //1증가
				int su = Integer.parseInt(str.substring(0, 2));
				a[i]=su; //i=0
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		} 
		call1(a);
	}
	private static void call1(int[] a) {
		int hap=0;
		for(int i=0;i<=4;i++) {
			hap=hap+a[i];
		}
		System.out.println("합 = "+hap);
	}
}//클래스종료

 

package day16_평가피드백;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ArrayTest2 {
	public static void main(String[] args) throws FileNotFoundException {
		String [] name = new String[100]; // 고정방식
		FileReader fr = new FileReader("c:/data/12261.txt");
		BufferedReader br = new BufferedReader(fr);
		int i=-1;
		while(true) {
			try {
				String str=br.readLine();
				if(str==null)break;
				i++; //1증가
				String bb = str.substring(0, 3);
				name[i]=bb; //i=0
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		} 
		call1(name,i); //배열의 이름을 다른 메소드한테 넘겨줌
	}
	private static void call1(String[] name,int su) {
		for(int i=0;i<=su;i++) {
			System.out.println(name[i]);
		}
	}
}//클래스종료

package day16_평가피드백;

public class ArrayTest3 {
	public static void main(String[] args) {
//		int [] a = new int[5];
		int [] a = {45,88,66,33,99}; //배열초기화
		another(a);
	}
	private static void another(int[] a) {
		AAA a2 = new AAA();
		a2.korea(a);
		System.out.println("두번째 호출력");
		for(int i=0;i<a.length;i++) {
			System.out.println(a[i]+"점");
		}
	}
}//클래스종료

 

package day16_평가피드백;

public class AAA {
	public void out() {
		
	}
	public void korea(int[] a) {
		System.out.println("첫번째 호출력");
		for(int i=0;i<a.length;i++) {
			System.out.println(a[i]+"점");
		}
	}
}

package day16_평가피드백;

public class ArrayTest3 {
	public static void main(String[] args) {
//		int [] a = new int[5]; // 숫자 첨자가 1개..1차원 배열
		int [] a = {45,88,66,33,99}; //점수 5개,석차를 구하기 1등~5등 5개 필요
		//a 배열명 점수 5개 a[0]~~a[4]  r[0]~~r[4]
		int [] r = new int[5];//점수에 따른 석차 1-5등 기억
		another(a,r);
	}
	private static void another(int[] a, int[] r) {//석차
		for(int i=0;i<=4;i++) { //기준
			r[i]=1;
			for(int j=0;j<=4;j++) { //비교
				if(a[i]<a[j]) { //기준점수가 비교점수
					r[i]=r[i]+1; //석차가 1씩 증가
				}
			}
		}
		System.out.println("점수\t석차");
		System.out.println("==========");
		for(int i=0;i<=4;i++) {
			System.out.println(a[i]+"\t"+r[i]);
		}
	}
}//클래스종료

 

package day16_평가피드백;

public class ArrayTest3 {
	public static void main(String[] args) {
		int [][] a = new int[5][2]; //5행(층) 2열(호) 0열..점수 1열..석차
		a [0][0] =45;
		a [1][0] =88;
		a [2][0] =66;
		a [3][0] =33;
		a [4][0] =99;
		callto(a);
	}
	private static void callto(int[][] a) {
		for(int i=0;i<=4;i++) { //기준
			a[i][1]=1; //기준 변경시 항상 1등
			for(int j=0;j<=4;j++) { //비교
				if(a[i][0]<a[j][0]) //기준점수가 비교점수보다 작으면
				{
					a[i][1]=a[i][1]+1; //석차가 1씩 증가
				}
			}
		}
		System.out.println("점수\t석차");
		System.out.println("==========");
		for(int i=0;i<=4;i++) {
			for(int j=0;j<=1;j++) {
				System.out.print(a[i][j]+"\t");
			}
			System.out.println();
		}
	}
}//클래스종료

package day16_평가피드백;

public class ArrayTest4 {

	public static void main(String[] args) {
		String [] name = {"홍길동","김김김","박박박","최최최","문문문"};
		int [] a = {66,77,99,88,90};
		int [] r = new int[5];
		another(name,a,r);
	}
	private static void another( String[] name, int[] a, int[] r) {
		for(int i=0;i<=4;i++) {
			r[i]=1;
			for(int j=0;j<=4;j++) {
				if(a[i]<a[j]) {
					r[i]=r[i]+1;
				}
			}
		}
		System.out.println("이름\t점수\t석차");
		System.out.println("======================");
		for(int i=0;i<=4;i++) {
			System.out.println(name[i]+"\t"+a[i]+"\t"+r[i]);
		}
	}
}

 

package day16_평가피드백;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ScoreDTO {
	public static void main(String[] args) throws FileNotFoundException {
		String [] name = new String [1000]; 
		int [] [] jum = new int[1000][7];
		FileReader fr = new FileReader("c:/data/12263.txt");
		BufferedReader br = new BufferedReader(fr);
		int po=-1;
		while(true) {
			try {
				String str = br.readLine();
				if(str==null)break;
				po++; // 0 1 2 3 4 5 배열 위치지정변수
				String ir=str.substring(0, 3);
				name[po]=ir; //배열 첫..이름을 저장
				int kor=Integer.parseInt(str.substring(3, 6));
				int eng=Integer.parseInt(str.substring(6, 9));
				int math=Integer.parseInt(str.substring(9, 12));
				jum[po][0]=kor;
				jum[po][1]=eng;
				jum[po][2]=math;
				jum[po][3]=kor+eng+math;
				jum[po][4]=jum[po][3]/3;
				if(jum[po][4]>=90)jum[po][5]=65; //A
				else if(jum[po][4]>=80)jum[po][5]=66; //B
				else if(jum[po][4]>=70)jum[po][5]=67; //C
				else if(jum[po][4]>=60)jum[po][5]=68; //D
				else jum[po][5]=70; //F
				}
			catch (IOException e) {
				System.out.println("오류");
			}
		}
		rank(name,jum,po); //rank 함수호출하면서 이름배열명,jum
	}
	private static void rank(String[] name, int[][] jum, int po) {
		for(int i=0;i<=po;i++) { //기준
			jum[i][6]=1;
			for(int j=0;j<=po;j++) { //비교
				if(jum[i][3]<jum[j][3]) { //기준총점이 비교총점보다 작으면
					jum[i][6]=jum[i][6]+1; //석차 1씩 증가
				}
			}
		}
		//석차
		System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t학점\t석차");
		System.out.print("==============================");
		System.out.println("==============================");
		for(int i=0;i<=po;i++) {
			System.out.print(name[i]+"\t");
			for(int j=0;j<=6;j++) { //0열(국어) 6열(석차)까지
				if(j==5)System.out.print((char)jum[i][j]+"\t");
				else
				System.out.print(jum[i][j]+"\t");
			}
			System.out.println();
		}
	}
}

'JAVA' 카테고리의 다른 글

221228_SingleTon  (0) 2022.12.28
221227_static  (0) 2022.12.27
221223_상속  (0) 2022.12.23
221222_상속  (0) 2022.12.22
221221_Interface,상속  (0) 2022.12.21

댓글