일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 웹
- java기초
- String
- 리눅스
- Spring
- javabasic
- DB
- 디자인패턴
- 직장인자기계발
- 자바
- 패스트캠퍼스후기
- 패스트캠퍼스
- 스프링
- 데이터베이스
- 국비
- 자바예제
- 디자인
- ncs
- 자바연습문제
- java
- 패캠챌린지
- 자바기본
- 직장인인강
- js
- 한번에끝내는JavaSpring웹개발마스터초격차패키지Online강의
- linux
- DesignPattern
- 자바기초
- 재택근무
- 한번에끝내는JavaSpring웹개발마스터초격차패키지Online
Archives
- Today
- Total
FIF's 코딩팩토리
자바 기초 NCS교육과정(4)-조건문if 본문
반응형
이번 시간은 if문에 대해 알아보겠습니다.
Java017_if.java
제어문(control statement): 문장의 흐름을 제어해주는 기능이다. 1. 종류 조건문 : if~else, switch~case 반복문 : for, while, do~while 기타 : break,continue,(label) if(조건식){ 참일때 수행할 문장; }else{ 거짓일때 수행할 문장; } |
public class Java017_if { public static void main(String[] args) { //int num=3;//1 int num=10; if(num%2==0)/*2*/ { System.out.printf("%d는 %s입니다.\n",num,"짝수"); }else {/*3*/ System.out.printf("%d는 %s입니다.\n",num,"홀수"); } }//end main() }//end class
Java018_if.java
조건식을 만족할때만 수행할 문장이 있는 경우 if(조건식){ 수행할 문장; } |
public class Java018_if { public static void main(String[] args) { int num = -10; if (num > 0) { System.out.printf("%d는 자연수 입니다.\n", num); } System.out.println("program end"); System.out.println("====================="); int data = 9; if (data % 3 != 0) { System.out.printf("%d는 3의 배수가 아닙니다.\n", data); } // end if }// end main() }// end class
Java019_if.java
if 안에 if
|
public class Java019_if { public static void main(String[] args) { boolean member = true; // 회원 or 비회원 String grade = "일반"; // 회원등급 // 회원이고 vip고객이면 30%적립, 회원이고 vip고객이 아닐때는 10%적립 // 회원이면 if (member) { // 회원이고 vip 고객이면.... if (grade == "vip") { System.out.println("30%적립"); } else { // 회언이지만 vip고객이 아니면... System.out.println("10%적립"); } } else { // 비회원이면 System.out.println("비회원"); } System.out.println("고객님 감사합니다."); }//end main() }//end class
Java020_if.java
다중 if~else if(조건식1){ 수행할 문장1; }else if(조건식2){ 수행할 문장2; }else if(조건식3){ 수행할 문장3; }else{ 수행할 문장; } |
public class Java020_if { public static void main(String[] args) { char code = 'B'; if(code=='A') { System.out.println("식품류"); }else if(code=='B') { System.out.println("육류"); }else if(code=='C') { System.out.println("야채류"); }else { System.out.println("기타"); } System.out.println("코드별 제품종류 체크"); }//end main() }//end class
Java021_if.java
문제를 풀어보세요! 충분히 고민한뒤, 코드를 보세요!
1. data변수에 저장된 값이 영문자 대문자이면 "?는 대문자입니다", 소문자이면 "?는 소문자입니다" 그외는 "?는 기타입니다"로 출력하는 프로그램을 구현하시오 2. [출력결과] data='D' => D는 대문자입니다. data='d' => d는 소문자입니다. data='1' => 1는 기타입니다. |
public class Java021_if { public static void main(String[] args) { char data = 'd'; if(data>='A' && data<='Z') { System.out.println(data+"는 대문자입니다."); System.out.printf("%c는 대문자입니다.",data); }else if(data>='a' && data<='z') { System.out.println(data +"는 소문자입니다."); System.out.printf("%c는 소문자입니다.",data); }else { System.out.println(data+"는 기타입니다."); } System.out.printf("\n%d\n",(int)'A'); }//end main() }//end class
Java022_if.java
키워드(예약어) : 프로그램에서 어떤 의미를 부여해서 정의해 놓은 단어 return : 현재 수행중이 메소드(main)을 완전히 빠져나올때 사용되는 키워드이다. |
public class Java022_if { public static void main(String[] args) { char check = 'n'; if(check=='n') { System.out.println("main종료"); return; } System.out.println(check); System.out.println("program end"); }//emd main() public static int process() { return 10; }//end process() }//end class
Java023_if.java
문제를 풀어보세요! 충분히 고민한뒤, 코드를 보세요!
각 월의 마지막일 1 3 5 7 8 10 12 => 31 4 6 9 11 =>30 2 => 28 |
public class Java023_if { public static void main(String[] args) { int month = 13; //월 int lastDay = -1;//마지막일 if(month<1 || month>12) { System.out.println("1~12까지만 입력하세요."); return; } if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) { lastDay=31; }else if(month==4 || month==6 || month==9 || month==11) { lastDay=30; }else if(month==28) { lastDay=28; } System.out.printf("%d월의 마지막 일은 %d입니다.\n",month,lastDay); }//end main() }//end class
Java024_if.java
문제를 풀어보세요! 충분히 고민한뒤, 코드를 보세요!
[출력결과] station = "KBS", channel = 7 => 닥터포스터 station = "KBS", channel = 9 => 국수의 신 station = "MBC", => 몬스터 station = "EBS", => 한국기행 |
public class Java024_if { public static void main(String[] args) { int channel = 9; String station = "KBS"; if (station == "KBS") { if (channel == 7) { System.out.println("닥터포스터"); } else if (channel == 9) { System.out.println("국수의신 "); } } else if (station == "MBC") { System.out.println("몬스터"); } else if (station == "EBS") { System.out.println("한국기행"); } }//end main() }//end class
반응형
'Back-End > 국비 NCS교과과정' 카테고리의 다른 글
자바 기초 NCS교육과정(6)-반복문 for (0) | 2019.07.03 |
---|---|
자바 기초 NCS교육과정(5)-분기문 switch~case (0) | 2019.07.03 |
자바 기초 NCS교육과정(3)-문제풀이 (0) | 2019.07.03 |
자바 기초 NCS교육과정(2)-연산자 (0) | 2019.07.02 |
자바 기초 NCS교육과정(1)-hello start! (0) | 2019.07.02 |