언어/Java

[자바스터디] 생활코딩 48강-53강 정리

qkrgusqls 2023. 3. 21. 19:26

 

배열

String[] classGroup = { "최진혁", "최유빈", "한이람", "이고잉" } ;
System.out.println(classGroup[0]);
System.out.println(classGroup[1]);
System.out.println(classGroup[2]);
System.out.println(classGroup[3]);
 

최진혁[0]

최유빈[1]

한이람[2]

이고잉[3]

으로 출력

 

 String[] classGroup = new String[4];
 classGroup[0] = "최진혁";
 System.out.println(classGroup.length);
 classGroup[1] = "최유빈";
 System.out.println(classGroup.length);
 classGroup[2] = "한이람";
 System.out.println(classGroup.length);
 classGroup[3] = "이고잉";
 System.out.println(classGroup.length);
 

length 는 classGroup에 수용가능한 값을 출력

4

 

 String[] members = { "최진혁", "최유빈", "한이람" };
        for (String e : members) {
            System.out.println(e + "이 상담을 받았습니다");
 

for문의 간편실행

e라고 하는 변수에 담아주기

 

예제

 

<1>

	public static void main(String[] args) {
		int [][]gugudan = new int[10][10];
		for(int i=1;i<gugudan.length; i++) {
			for(int j=1;j<gugudan.length;j++) {
				gugudan[i][j] = i*j;
			System.out.print(i + "x" + j + "=" +gugudan[i][j] + " ");
			}
			System.out.print("\n");
		}
	}
 

 

<2>

	public static void main(String[] args) {
		 Scanner sc=new Scanner(System.in);  
		 
		 int[] num = new int[5];        
		 int max = 0;
		                 //Ctrl+shift+o누르면 자동으로 import설정
		 System.out.print("5개의 정수를 입력하세요 : ");
		 for(int i=0; i<num.length; i++)
		 {
		 num[i]=sc.nextInt();                           
		 }
		 
		 for(int i=0; i<num.length; i++) {
			if(num[i] > max) max = num[i];
		 }
		 
		  System.out.println("가장 큰 수는" + max +"입니다."); 
		}
 

 

<3>

public static void main(String[] args) {
		 int[] score = new int[5];    
		 int sum=0;                    
		 Scanner sc=new Scanner(System.in);                  
		 System.out.print("5개의 정수를 입력하세요 : ");
		 for(int i=0; i<score.length; i++)
		 {
		 score[i]=sc.nextInt();             
		 sum=sum+score[i];               
		 }
		  System.out.println("입력한 정수의 평균은 :" +(sum/score.length)+"입니다."); 
		}
	}
 

<4>

 

public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		
		int count = 0; 
		int monthSet[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
		
		System.out.println("월, 일?");
		int m = sc.nextInt();
		int d = sc.nextInt();
		
		
		if(m==0 && d ==0) {
			System.out.println("종료합니다.");
		}
		
		if(m<1 || m>12 ||d>monthSet[m-1]) {
			System.out.println("잘못된 입력입니다.");
		} else {
            for (int i = 0 ; i < m - 1 ; i++) {
                count += monthSet[i];      
            	}
            	count += d;            
            System.out.println(m+"월"+ d+"일은" + count+ "일째 입니다.");
            }
			
		}
	}