자바실습 예제

//IFelse.java class IFelse { public static void main(String args[]) { int month = Integer.parseInt(args[0]); // 실행 명령에서 사용자로부터 데이터를 입력받아 // 정수로 변환한 다음 momth에 저장 String MtoS; if(month == 12 || month == 1 || month == 2) MtoS = "겨울"; else if(month == 3 || month == 4 || month == 5) MtoS = "봄"; else if(month == 6 || month == 7 || month == 8) MtoS = "여름"; else if(month == 9 || month == 10 || month == 11) MtoS = "가을"; else MtoS = "해당되는 계절이 없습니다."; System.out.println( month + " 월은 " + MtoS + " 입니다."); } } //Swich.java class Switch { public static void main(String args[]) { for(int i=1; i<5; i++) switch(i) { case 1: System.out.println(i +" : 가위 "); break; case 2: System.out.println(i + " : 바위"); break; case 3: System.out.println(i + " : 보"); break; default: System.out.println(i + "는 1에서 3사이의 값이 아닙니다."); } } } //NestedForBreak.java class NestedForBreak { public static void main(String args[]) { int i, j; for(i=1 ; i<10 ; i++) { for(j=1 ; j < i ; j++) { if (j > 6) break; System.out.print(" * "); } System.out.println(); } } } //BreakTest.java class BreakTest { public static void main(String args[]) { boolean t = true; first: { second: { third: { System.out.println("third 블록 'break' 문장전"); if(t) break second; // second 블럭 밖으로 제어가 이동 System.out.println("third 블록 'break' 문장후"); } System.out.println("second 블록 문장"); } System.out.println("first 블록 문장"); } } } //ContinueLabelTest.java class ContinueLabelTest { public static void main(String args[]) { outer: for (int i=0; i<10; i++) { for(int j=0; j<10; j++) { if(j > i) { System.out.println(); continue outer; // outer로 지정된 for 블록 처음으로 제어 이동 } System.out.print(" " + (i * j)); } } } } //Return.java class Return { public static void main(String args[]) { boolean t = true; System.out.println("알기 쉽게 해설한 자바!"); if(t) return; System.out.println("자바를 배워봅시다."); } }