Recent Posts
Recent Comments
Link
06-30 12:53
Today
Total
관리 메뉴

삶 가운데 남긴 기록 AACII.TISTORY.COM

조건문과 제어문 본문

DEV&OPS/Java

조건문과 제어문

ALEPH.GEM 2022. 3. 25. 14:27

if~else

class Sample {
	public static void main(String[] args){
		int num = (int)(Math.random()*6) +1;	//주사위 번호 뽑기
		
		if(num == 1) {
			System.out.println("1번이 나왔습니다.");
		}else if(num == 2){
			System.out.println("2번이 나왔습니다.");
		}else {
			System.out.println("3번이상이 나왔습니다.");
		}
	}
}

 

switch

class Sample {
	public static void main(String[] args){
		int num = (int)(Math.random()*6) +1;	//주사위 번호 뽑기
		
		switch(num) {
		case 1:
			System.out.println("주사위 1");
			break;
		case 2:
			System.out.println("주사위 2");
			break;
		default:
			System.out.println("3이상의 주사위");
			break;				
		}
	}
}

 

for: 반복 횟수를 알고 있는 경우 반복문

class Sample {
	public static void main(String[] args){
		for(int m=2; m<=9; m++) {
			for(int n = 1; n <= 9; n++) {
				System.out.println(m + "x" + n +"=" + (m*n));
			}
		}
	}
}

 

향상된 for문: 컬렉션 항목 개수만큼 반복

class Sample {
	public static void main(String[] args){
		int[] arrScore = {91, 72, 86, 95, 87};
		int sum = 0;
		for(int score: arrScore) {
			sum = sum +score;
		}
		System.out.println("총 점수: "+sum);		
	}
}

 

while: 조건식이 true인 동안 반복

import java.io.IOException;

class Sample {
	public static void main(String[] args) throws IOException{
		boolean isRun = true;
		int speed = 0;
		int keyCode = 0;
		while(isRun) {
			if(keyCode!=13 && keyCode!=10) {
				System.out.println("1.가속 2.감속 3.중지");
				System.out.println("선택: ");
			}
			keyCode = System.in.read();	//키보드의 키코드를 읽음
			
			if(keyCode == 49) {
				speed++;
				System.out.println("현재 속도="+speed);
			}else if(keyCode == 50) {
				speed--;
				System.out.println("현재 속도="+speed);
			}else if(keyCode == 51) {
				isRun = false;
			}
		}
		System.out.println("종료");
	}
}

 

자바 키코드

  • 숫자키 0~9 : 키코드 48~57
  • 알파벳키 A~Z:  키코드 65~90
  • 알파벳키 a~z: 키코드 97~122
  • Backspace : 8
  • Tab : 9
  • Enter: 캐리지리턴13, 라인피드10
  • Shift: 16
  • Ctrl: 17
  • Alt: 18
  • Space: 32
  • Pageup , Pagedown: 33, 34
  • 방향키 좌, 상, 우, 하: 37, 38, 39, 40

 

do ~ while: do 블록을 먼저 실행 후 while의 조건이 true인 동안 반복

import java.io.IOException;
import java.util.Scanner;

class Sample {
	public static void main(String[] args) throws IOException{
		System.out.println("메시지를 입력하세요.");
		System.out.println("종료하려면 q를 입력하세요.");
		Scanner scanner = new Scanner(System.in);
		String inputString;
		
		do {
			System.out.print(">");
			inputString = scanner.nextLine();
			System.out.println(inputString);
		}while(!inputString.equals("q"));
		
		System.out.println();
		System.out.println("종료");
				
	}
}

 

break, continue

break: 반복/조건 문을 빠져나옴

continue: 다음 반복 조건으로

class Sample {
	public static void main(String[] args){
		for(int i=1; i<=10 ; i++) {
			if(i%2 != 0) {
				continue;
			}
			System.out.println(i);
		}
				
	}
}

 

 

 

 

 

 

 

 

 

 

728x90

'DEV&OPS > Java' 카테고리의 다른 글

추상 클래스와 인터페이스  (0) 2022.03.31
자바 클래스와 메서드  (0) 2022.03.28
자바 연산자  (0) 2022.03.24
자바 자료형과 변수  (0) 2022.03.22
JDK  (0) 2022.03.22