메뉴 건너뛰기

2023.02.15 17:05

조건문과 제어문

조회 수 61 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

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);
}

}
}

 


List of Articles
번호 제목 날짜 조회 수
151 A java Runtime Environment(JRE) or Java Development Kit(JDK) must be ~~~~ 하면서 이클립스가 실행안될때. file 2019.03.05 789
150 Apache Commons HttpClient 3.x 로 Http 서버에 파일 전송하기 file 2019.01.08 1136
149 BigDecimal타입의 사칙연산 2016.12.22 3713
148 Database Connections 생성하기 (Mysql) file 2016.08.29 3365
147 Database Connections 생성하기 (오라클) file 2016.08.29 4072
146 eclipse 콘솔(로그)에 디버그(Debug) 모드에서 실행된 쿼리문을 보여주자. - 전자정부프레임워크 오라클 file 2016.08.29 4172
145 eclipse 콘솔(로그)에 디버그(Debug) 모드에서 실행된 쿼리문을 보여주자. - 전자정부프레임워크 오라클 file 2016.08.29 6001
144 Eclipse에서 import문 자동으로 작성하는 기능 file 2016.09.19 4811
143 enum 2016.09.13 3404
142 File 클래스 file 2016.09.13 3603
141 for-each문 file 2016.09.13 3120
140 GET 파라메타 쿼리문자열 컨트롤 : getQueryString 2016.12.09 4705
139 Gmail 메일 서버를 이용해서 메일 보내기 file 2020.06.29 256
138 HashMap 사용하기 file 2021.03.31 134
137 iBATIS 동적으로 맵핑하기 2016.12.09 3949
136 Jadclipse 플러그인 설치 file 2016.09.19 3512
135 JAR 파일 2016.09.19 3713
134 Java : JSOUP 를 이용, html에서 소스, 링크경로 추출후 절대 경로로 바꾸기 2019.01.08 1166
133 JAVA CentOS JDK 설치 및 환경변수 설정 file 2018.07.09 1821
132 JAVA HashMap의 Key값 출력하기 2018.07.09 1079
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved