본문 바로가기
Java

JAVA Switch case , for while, Array , func

by leopard4 2023. 1. 18.
public class Switch {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int a = 3;
		
		switch(a) {
		case 1: 
			System.out.println("hello");
			break;
		case 2:
			System.out.println("Bye");
			break;
		case 3:
			System.out.println("Good~");
			break;
		case 4:
			System.out.println("Nice~");
			break;
		default :
			System.out.println("the end");
			break;
		}
		
		String month = "4월";
		
		switch(month){
			case "1월":
			case "2월":
			case "12월":
				System.out.println("겨울");
				break;
			case "3월": case "4월": case "5월":
				System.out.println("봄");
				break;
		}
	
	}

}

 

public class Loop {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//1
		//2
		//3
		//4
		//5
		System.out.println(1);
		System.out.println(2);
		System.out.println(3);
		System.out.println(4);
		System.out.println(5);
		
		// 반복할 변수의 초기값 
		// 반복 조건
		// 반복할 변수
		// i++ 문법은, i의 값에 1을 더한후, 다시 i에 저장
		for( int i = 0 ; i <= 4 ; i++ ) {
			int data = i + 1;
			System.out.println(data);
		
		}
		// 1 ~ 100 까지의 홀수를 다 더하세요. i--도 가능
		int total = 0;
		for(int i = 1 ; i<101 ; i++) {
			if ( i % 2  ==  1 ) {
				System.out.println(i);
				total = total + i;
				System.out.println(total);
			}
		}
		int i = 1;
		while( i <= 5 ) {
			System.out.println(i);
			i++;
		}
		// 1 ~ 100 까지의 짝수를 다 더하세요. while로
		i = 1;
		total = 0;
		while( i <= 100 ) {
			if( i % 2 == 0) {
				total = total + i ;
			}
			i++;
		}
		System.out.println(total);
		
		// 1 ~ 100 까지 다 더하는데
		// 합이 78보다 크면 멈춘다.
		total = 0;
		for(int k = 1 ; k <= 100 ; k++) {
			total = total + k;
			if (total > 78) {
				break;
		}
		
		}
	
		System.out.println(total);
	
		// 무한루프
		
		while (true) {
			System.out.println("무한루프");
		}
	}
	
	
	
	
}

 

 

public class Array {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// 데이터를 여러개 저장하는 것!
		// 자바의 가장 기본적인, 데이터 여러개 저장하는
		// 데이터 스트럭쳐는 Array 다.

		// 하지만 자바의 Array 는 사용하기가 귀찮다.
		// 따라서 실무에서는 ArrayList 와 HashMap을
		// 사용한다.

		// 학생들 50명의 수학점수를 처리하려 한다.
		int score1 = 70;
		int score2 = 90;
		int score3 = 77;

		// 자바의 Array 는, 저장할 데이터 갯수!
		// 를 꼭 지정해야 한다.
		int[] scoreArray = new int[50] ;
		scoreArray[0] = 70;
		scoreArray[1] = 90;
		
		// 학생들 20명의 점수를 관리하려 한다.
		// 반복문을 사용하여, 모든 학생의 점수를
		// 30으로 저장하세요.
		int[] studentArr = new int[20] ; 
		for(int i = 0 ; i <= 19 ; i++ ) {
			studentArr[i] = 30;
			System.out.println(studentArr);
		}
		
		// 3번째 학생의 점수를 출력하시오.
		System.out.println(studentArr[2]);
		
		// 20명 학생의 점수를 모두 출력하시오.
		for(int i = 0; i < studentArr.length ; i++) {
			System.out.println(studentArr[i]);
		}
		
		// 30 명의 영어점수를 저장하려 합니다.
		// 이 중에서, 인덱스가 7의 배수가 되는 학생들에게는 가산점이 있으므로
		// 40점으로 저장하고
		// 나머지 학생들은 30점으로 저장하세요.
		
		int[] engArr = new int [30];
		for(int i = 0 ; i< engArr.length; i++) {
			if( i % 7 == 0) {
				engArr[i] = 40;
			}else {
				engArr[i] =	30;
				}
			}
		System.out.println(engArr[7]);
		System.out.println(engArr[11]);
		System.out.println(engArr.length);
		
		
		// 70, 99, 45 ,64
		// 데이터를 바로 배열로 만드는 방법
		
		int[] mathScoreArr = {70, 99 , 45 , 64};
		
		System.out.println(mathScoreArr[1]);
		System.out.println(mathScoreArr.length);
		
		}
	
}

 

public class FuncTest2 {
	
	
	int add(int a , int b) {
		int total = a + b; 
		return total;
	}
	
	String getGrade(int size) {
		
		if (size >= 16 && size <= 30) {
			return "A";
		}else if (size >= 8 && size <= 15) {
			return "B";
		}else if (size >= 1 && size <= 7){
			return "C";
		}else {
			return "사이즈가 이상합니다.";
		}
	}
			
		// 숫자를 입력받으면, 해당 숫자만큼
		// 화면에 "안녕하세요~" 를 출력하는 함수를 만드세요.
		// printHello(3)
	
	void printHello(int count) {
		
		for (int i = 0 ; i < count ; i++) {
			
		System.out.println("안녕하세요~");
		}
	}
				
	// getNumber(2 , 3); => 8
	int getNumber(int a, int b) {
		int result = 1;
		for(int i = 0 ; i < b ; i++) {
			result = result * a;
		}
		return result;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		FuncTest2 ft = new FuncTest2();
		
		System.out.println(ft.add( 3, 4 ));
		int result = ft.add(5, 10);
		System.out.println(result);
		
		ft.getGrade(17);
		System.out.println("등급은 : " + ft.getGrade(40));
		
		ft.printHello(8);
		
		System.out.println(ft.getNumber(3,4));
		
	}

}