3장 반복문과 배열, 예외처리(1차원 배열과 for-each)
이론:
- 1차원 배열과 for-each
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
int[] iArray = new int[10]; | |
String[] strArray = new String[]{"감", "배", "사과", "포도"}; | |
for (int i = 0; i < iArray.length; i++) { | |
iArray[i] = i * 2; | |
} | |
for (int i : iArray) { | |
System.out.print(i + ","); | |
} | |
System.out.println(); | |
for (String str : strArray) { | |
System.out.print(str + ","); | |
} | |
} | |
/* output: | |
0,2,4,6,8,10,12,14,16,18, | |
감,배,사과,포도, | |
*/ |
실습:
단어 5개를 입력받아서 역순으로 출력하는 프로그램을 작성하시오.
예)
감 배 사과 포도 수박
수박 포도 사과 배 감
예)
감 배 사과 포도 수박
수박 포도 사과 배 감