조건문 (if, else, else if)
다중 조건문 : if와 else 사이에 else if를 사용하여 조건을 더 부여할 수 있다
if(논리식1) {행동1} //논리식 1을 만족할 때 {}를 실행
else if(논리식2) {행동2} //논리식 2을 만족하지 않은 경우 논리식 3를 만족하면 {}를 실행
else {행동3} //논리식 1과 논리식 2를 모두 만족하지 못했다면 {}를 실행
** 어떤 값이 if의 조건을 만족하는 경우 해당하는 행동1을 하며 만족하지 못할 경우
** else if의 조건을 만족하는지 검사하여 만족하는 경우 해당하는 행동2을 하며 만족하지 못할 경우
** else에 해당하는 행동3을 한다
day04 - condition - Test08 : 다중 조건 예제
day04 - condition - Test09 : 지하철 요금 계산기
day04 - condition - Test10 : SNS 게시글 시간 표시
조건문 (switch ~ case)
int number = 2; //변수 설정
switch(number) { //조건 검사를 할 변수 설정
case 1:
System.out.println("number = 1");
break;
case 2:
System.out.println("number = 2");
break;
case 3:
System.out.println("number = 3");
break;
case 4:
System.out.println("number = 4");
break;
}
- switch(변수명) : 조건 검사를 할 변수 설정
- case 숫자 데이터 : switch에서 지정한 변수가 해당 case의 숫자 데이터와 일치하는지 검사 (모든 case를 동시에 검사)
- break : 조건을 만족하는 case를 찾으면 실행을 종료
★ switch ~ case 조건문의 동작
조건을 만족 : switch에서 정한 변수의 숫자 데이터와 case의 값이 같을 때
1) switch에서 정한 변수가 조건을 만족하는 첫 번째 case부터 마지막 case까지 선택
2) break;가 입력되어있다면 조건을 만족하는 첫 번째 case부터 break까지 선택
** break를 입력하지 않을 경우 case 2:부터 case 4:에 해당하는 내용을 출력
switch ~ case 조건문과 if 조건문과의 차이
switch ~ case 조건문 | if 조건문 |
- 모든 case가 동시에 조건 검사 - 속도가 느리다 |
- 각각의 조건을 단계적으로 검사 - 속도가 빠르다 |
day04 - condition2 - Test01 : if문의 구조적 문제
day04 - condition2 - Test02 : switch문의 특징
day04 - condition2 - Test03 : 월별 일수 구하기
day04 - condition2 - Test04 : 윤년 판정
I / O (Input / Output)
입력과 출력의 구분 : CPU를 기준으로 한다
- STD I / O : System.in과 System.out
- File I / O : 파일 열기(in)과 파일 저장(out)
- Network I / O
STD I / O (Standard I / O, 표준 입출력)
System.out 출력
System.out.print(); //() 안의 값을 출력
System.out.print("hello"); //출력 : hellohello
System.out.print("hello");
System.out.println(); //() 안의 값을 출력한 후 줄바꿈
System.out.println("hello"); //출력 : hello
System.out.println("hello"); // hello
System.out.printf(); //()C언어를 위한 출력
System.out.printf("%d + %d = %d \n", 10, 20, 30); //출력 : 10 + 20 = 30
day04 - io - Test01 : 표준 출력 명령
System.in 입력
package 패키지명;
import java.io.IOException;
import java.lang.*;
public class 클래스명 {
public static void main(String[] args) throws IOException {
int a = System.in.read();
System.out.println("a =" + a);
int b = System.in.read();
System.out.println("b =" + b);
int c = System.in.read();
System.out.println("c =" + c);
int d = System.in.read();
System.out.println("d =" + d);
int e = System.in.read();
System.out.println("e =" + e);
}
}
입력으로 21을 입력하고 Enter를 누르면 2의 ASCII 번호 50 1의 ASCII 번호 49 Enter는 Carriage Return(해당 줄 맨 앞으로 이동) 후 Line Feed(줄바꿈)로 인식되어 CR의 ASCII 번호 13 LF의 ASCII 번호 10 |
출력값은 다음과 같다 a = 50 b = 49 c = 13 d = 10 |
문제점
1. 하나의 명령이 한 글자만 입력받는다
2. 입력을 ASCII 코드표 숫자로 변환해서 받는다
3. 예외 처리를 선택해야 한다 (throws IOException 부분)
4. 자료형을 선택할 수 없다
day04 - io - Test02 : System.in을 단독으로 쓰지 않는 이유
Scanner를 사용한 입력
1) import java. util. Scanner;
2) Scanner 스캐너명 = new Scanner(System.in);
3) sc.close();
package io;
import java.lang.*;
import java.util.Scanner; //Scanner를 위한 import
public class Test03 {
public static void main(String[] args) {
//Scanner 생성
Scanner sc = new Scanner(System.in); //new는 오른쪽에 제시된 도구를 생성하는 명령
//정수 입력
int a = sc.nextInt(); //정수 입력까지 대기중
System.out.println("a = " + a);
//실수 입력
float b = sc.nextFloat(); //실수 입력까지 대기중
System.out.println("b = " + b);
//실수 입력
double c = sc.nextDouble(); //실수 입력까지 대기중
System.out.println("c = " + c);
//Scanner 종료
sc.close();
}
}
Scanner를 이용한 숫자 입력 변수 설정
//정수
byte a = sc.nextByte();
short b = sc.nextShort();
int c = sc.nextInt();
long d = sc.nextLong();
//실수
float e = sc.nextFloat();
double f = sc.nextDouble();
day04 - io - Test03 : Scanner 사용법
day04 - io - Test04 : 중국집 계산기 + 입력
day04 - io - Test05 : BMI 계산
day04 - io - Test06 : PC방 요금 계산
Scanner를 이용한 문자열 입력 변수 설정
String f = sc.next();
day04 - io - Test07 : 문자열 입력
'국비교육 > 국비교육' 카테고리의 다른 글
Day6 - 0730 (0) | 2022.08.01 |
---|---|
day5 - 0729 (0) | 2022.07.29 |
Day3 - 0727 (0) | 2022.07.27 |
Day2 - 0726 (0) | 2022.07.26 |
Day1 - 0725 (0) | 2022.07.25 |