public class Sample {

	public static void main(String[] args) {
		//세사람 성적구하는 프로그램
		String[] name= {"홍길동" , "이순신" , "강감찬" };
		int[] kor = {90, 80, 95};
		int[] eng = {70, 80, 96};
		int[] mat = {88, 70, 50};
		
		for(int i =0; i<name.length; i++) {
			int tot = kor[i]+eng[i]+mat[i];
			double avg = tot/3.;
			System.out.println(name[i]+ "\t" + tot+ "\t" + avg);
		}
		System.out.println("=================================");
		String[] pname = {"세탁기" , "냉장고", "TV" };
		int[] price = {150, 300, 200};
		for(int i=0; i<pname.length; i++) {
			System.out.println(pname[i]+ "\t" +price[i]);
		}
	}
}

배열예제1

  • 배열의 길이 (배열이름.length) -> 인덱스 범위 에러방지를 위해서 사용한다.

댓글남기기