메뉴 건너뛰기

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
번호 제목 날짜 조회 수
31 [자바(스프링&mybatis&jsp) 프로젝트 & 아파치 &톰켓 연동 ] 이클립스 프로젝트 생성 순서08.commons-logging 설치 file 2016.08.18 4458
30 회원가입 양식에서 (필수/옵션)필드 추가/제거하기 file 2016.08.29 4471
29 자바용 날짜 계산 2016.09.19 4552
28 예외처리(Exception handling)방법 file 2016.09.21 4585
27 No bean name '***Service' is defined 오류나는 이유 및 해결방법 file 2016.08.29 4588
26 기존 프로젝트, Table, View 모두 다 삭제하기 file 2016.08.29 4610
25 GET 파라메타 쿼리문자열 컨트롤 : getQueryString 2016.12.09 4705
24 한글이 깨졌다! 인코딩(encoding)을 설정하자 file 2016.09.19 4717
23 이클립스 플러그인 삭제방법 file 2016.09.19 4724
22 간단한 개인홈페이지 만들어보기 file 2016.08.29 4753
21 Eclipse에서 import문 자동으로 작성하는 기능 file 2016.09.19 4815
20 자바 XML 제어 라이브러리 XStream : JAVA 2016.12.09 4918
19 다양한 문자 조합을 이용한 카운팅 구하기 2016.12.09 4941
18 MySQL에 All-in-one 설치시 webmaster로 로그인 안되는 문제 해결을 위한 2가지 수정사항 file 2016.08.29 4954
17 예외 만들기 2016.09.13 4961
16 전자정부프레임워크 v2.5, v2.6 오라클 세팅하기 file 2016.09.12 5109
15 접근 제한자 private/ protected/ public / default file 2016.10.06 5136
14 System.out.println(); 이클립스에서 자동화기능 사용 file 2016.09.19 5158
13 스프링(Spring)의 Validator 기능을 사용하다가 '정의되지 않음 또는 null 참조인 'type' 속성을 가져올 수 없습니다.'라는 오류를 만났을 때 해결방법 file 2016.08.29 5173
12 날짜 시간 포맷 제어 자바 클래스 : JAVA DateUtils Date time file 2016.12.09 5233
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved