이것이 자바다 책으로 switch case 문을 학습하고 있다.
if, else를 남발하지 않고도 짧은 코드량으로 원하는 결과를 얻을 수 있다. 특히 조건이 비교적 단순하고 비슷한 경우에 큰 효과를 발휘한다.
/** switch case + Math.random()*/package Java_ch4;public class SwitchExample {public static void main(String[] args) {int num = (int)(Math.random() * 6) + 1;switch (num) {case 1:System.out.println("dice number1");break;case 2:System.out.println("dice number2");break;case 3:System.out.println("dice number3");break;case 4:System.out.println("dice number4");break;case 5:System.out.println("dice number5");break;case 6:System.out.println("dice number6");break;}}}
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 | /* * switch case + String(java7~) */ package Java_ch4; public class SwitchStringExample { public static void main(String[] args) { String position = "CEO"; switch (position) { case "CEO": System.out.println("salary $20000"); break; case "manager": System.out.println("salary $10000"); break; case "employee": System.out.println("salary $5000"); break; } } } | cs |
'문돌이의 IT > 자바(Java)' 카테고리의 다른 글
방송통신대학교 Java프로그래밍 강의 정리2 (0) | 2017.03.04 |
---|---|
방송통신대학교 Java프로그래밍 강의 정리 (1) | 2017.03.03 |
자바(Java) 중첩 if 조건문 (2) | 2017.02.10 |
자바(Java) 조건문 if else, math.random() (0) | 2017.02.09 |
자바(Java) 조건문 if, if else (0) | 2017.01.05 |