본문 바로가기
JAVA SE/Code

[20.07.13/Day_04] Java SE / 연산자, 제어문(조건문,반복문)

by 파프리카_ 2020. 7. 13.
728x90
반응형

 

 

 

 


제어문

조건문

 

\MyAccount.java

package step4_accontment;

public class MyAccount {
	// 인스턴스 변수는 선언만 할 경우 기본 초기화 0으로 할당됨
	private int balance;

	public void setBalance(int balance) {
		// 한 줄 조건문은 {}를 안써줘도 된다.
		if (balance > 0)
			this.balance = balance;
		else
			System.out.println("잘못된 금액입니다.");
	}

	public int getBalance() {
		return balance;
	}

	// 입금 메서드 : 기존 balance(잔액)에 금액을 누적한다
	public void deposit(int money) {
		if (money > 0)
			balance += money;
		else
			System.out.println("잘못된 입금액입니다.");
	}
	
	// 출금 메서드
	public void withdraw(int money) {
		if (money > 0) 
			balance -= money;
		else
			System.out.println("잘못된 출금액입니다.");
	}

}

 

\TestIf3.java

package step4_accontment;

public class TestIf3 {
	public static void main(String[] args) {
		MyAccount account = new MyAccount();
		account.setBalance(100);
		System.out.println(account.getBalance()); // 100

		account.setBalance(-1); // 잘못된 입력값입니다.
		System.out.println(account.getBalance()); // 100: 기존 할당되었던 100이 그대로 출력된다.

		account.setBalance(500);
		System.out.println(account.getBalance()); // 500

		System.out.println("******입금******");
		int money = 100;
		account.deposit(money);
		System.out.println(account.getBalance()); // 600
		money = -1;
		account.deposit(money); // 잘못된 입금액입니다.
		System.out.println(account.getBalance());
		money = 400;
		account.deposit(money);
		System.out.println(account.getBalance()); // 1000
		
		System.out.println("******출금******");
		money = 20;
		account.withdraw(money);
		System.out.println(account.getBalance()); // 980
		money = -20;
		account.withdraw(money); //잘못된 출금액 입니다.
		System.out.println(account.getBalance()); 

	}
}

 

Switch

\TestSwitch2.java

package step5;

public class TestSwitch2 {
	public static void main(String[] args) {
		/*
		 * grade 1이면 하버드 
		 * 2 북경대 
		 * 3,4,5 카네기멜론 
		 * 나머지 스탠포드
		 */	
		int grade = 9;
		switch(grade) {
		case 1:
			System.out.println("하버드");
			break;
		case 2:
			System.out.println("북경대");
			break;
		case 3:case 4:case 5:
			System.out.println("카네기 멜론");
			break;
		default:
			System.out.println("스탠포드");
		}
	}
}

Switch & If else 

 

\DataService.java

package step5;

public class DateService {
	/*
	 * 2월 : 28일
	 * 4,6,9,11월 : 30일 
	 * 1,3,5,7,8,10,12월 : 31일 
	 * 그외 : 잘못된 입력값입니다.
	 */
	public void printLastDay(int month) {
		if (month >= 1 && month <= 12) {
			switch (month) {
			case 2:
				System.out.println(month + "월 마지막 일은 28일입니다.");
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				System.out.println(month + "월 마지막 일은 30일입니다.");
			default:
				System.out.println(month + "월 마지막 일은 31일입니다.");
			}
		}else {
			System.out.println("잘못된 입력값입니다.");
		}
	}

}

 

\TestSwitch.java

package step5;

public class TestSwitch3 {
	public static void main(String[] args) {
		// 7월 마지막 일은 31일입니다.
		int month = 7;
		DateService service = new DateService();
		service.printLastDay(month);
		
		// 2월 마지막 일은 28일입니다.
		month=2;
		service.printLastDay(month);
		
		// 잘못된 입력값입니다.
		month = 13;
		service.printLastDay(month);
		
	}
}

 

반복문

 

\TestWhile.java

package step6;

public class TestWhile {
	public static void main(String[] args) {
		int i = 1;
		while (i<10) {
			System.out.println(i+"잔");
			i ++; // 실행 후 1 증가
		}
	}//main
}//class

 

\TestFor.java

package step6;

public class TestFor {
	public static void main(String[] args) {
		for (int i = 1; i < 10; i++) {
			System.out.println(i+"잔");
		}
	}

}

 

\TestDoWhile.java

package step6;

public class TestDoWhile {
	public static void main(String[] args) {
		int i = 1;
		do { // 실행 후 조건검사(while문)으로 넘어가기 때문에
			 // 반드시 한번은 실행
			System.out.println(i);
		} while(i < 0);
	} 
}

 

For문 실습예제 !

/ForLoopService.java

package step7;

public class ForLoopService {

	public void printInfo(String item, int count) {
		for(int i=1; i<=count; i++) {
			System.out.println(item+" "+i+"잔을 마셨다.");
		}
	}
}

 

\TestFor2.java

package step7;

public class TestFor2 {
	public static void main(String[] args) {
		ForLoopService service = new ForLoopService();
		String item = "기네스";
		int count = 7;
		service.printInfo(item, count);
		/*
		 *  기네스 1잔
		 *  기네스 2잔
		 *  ...
		 *  기네스 7잔
		 */
	}
}

 

중첩반복문

 

\TestFor3.java

package step7;

public class TestFor3 {
	public static void main(String[] args) {
		/*    (3*5)
		 *    1 2 3 4 5 
		 *    1 2 3 4 5
		 *    1 2 3 4 5 
		 */
		int row = 3; // 행
		int col = 5; // 열
		System.out.println("아  \t이유");
		System.out.println();
		System.out.println("박  \t보검");
		System.out.println("\n");
		
		// 중첩 반복문
		for(int i = 1; i <= row; i++) {
			for(int j = 1; j <= col; j++) {
				System.out.print(j+"\t");
			}
			System.out.println("\n");
		}
	}

 

구구단 만들기 연습

 

package step7;

public class TestFor5_duplicated {
	public static void main(String[] args) {
		// 구구단 2단부터 9단까지 출력하는 예제
		// 중첩 for문을 이용한다
		for (int i=2; i<10; i++) {
			for (int j=1; j<10; j++) {
				int result = i*j;
				System.out.print(i+"*"+j+"="+result+"\t");
			}
			System.out.println();
		}
	}
}

 

 

728x90
반응형