// 반복문
//1. for문
for(int a = 1; a <= 3 ; a++) {
System.out.println("java");
}//for end
//System.out.println(a); 에러, for문 안에서 선언된 a는 for문 내에서만 사용가능
int b = 0;
for(b = 1; b <= 3 ; b++) {
System.out.println("python");
}//for end
System.out.println(b);//4, b는 for문 밖에서 선언되었으므로
//2. while문
int i = 0;
while(i <= 3) {
System.out.println("seoul");
i++;
}//while end
//3. do~while문
int j = 0;
do {
System.out.println("jeju");
j++;
} while(j <= 3);
//4. break와 continue문
for(int a = 1; a < 10; a++) {
if(a == 5) {
break;
}//if end
System.out.println(a + " ");
}//for end
System.out.println();//줄바꿈
for(int a = 1; a < 10; a++) {
if(a == 5) {
continue;
}//if end
System.out.println(a + " ");
}//for end
///////////////////////////////////////////////////////
o 연습문제
//문1)알파벳을 아래와 같이 한줄에 5개씩 출력하시오
/* ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z */
int count = 0;
for(char ch='A'; ch <= 'Z'; ch++) {
count++;
System.out.print(ch);
//System.out.print(count);
if(count % 5 == 0) {
System.out.println();
}//if end
}//for end
/////////만약 count가 5의 배수이면 줄바꿈
///////////////////////////////////////////////////////////////
//문2)아래와 같이 출력하시오
/* ####
###
##
# */
for(int a = 1; a <= 4; a++) {
for(int b = 1; b <= 4; b++) {
if(a <= b) {
System.out.print('#');
}else {
System.out.print(' ');
}//if end
}//for end
System.out.println();
}
/////////////////////////////////////////////////////


int sum = 0;
for(int a = 10; a <= 100; a += 10) {
for(int b = a-9; b <= a ; b++) {
sum += b;
}
System.out.printf("%d + ... + %d = %d\n", (a-9), a, sum);
sum = 0;
}//for end
/////////////////////////////////////
//문5)어느 달팽이는 낮에는 3cm올라가고, 밤에는 2.5cm 내려온다고 할때
// 달팽이가 13cm의 나무 꼭대기에 올라가려면 며칠이 걸리는지 구하시오
//답) 21일
int day = 0; //결과값
double snail = 0.0; //달팽이
while(true) {
day++;
snail = snail + 3.0;
if(snail >= 13.0) {
break;
} else {
snail = snail - 2.5;
}//if end
}//while end
System.out.println(day+"일");
////////////////////////////////////////
o 배열
//연속성 자료형, 열거형
//-> 1차원배열 : [열]
//-> 2차원배열 : [행][열]
//-> 3차원배열 : [면][행][열] (자바에 없음)
//-> new연산자 : 메모리 할당(확보) 연산자
/////////////////////////////////////
//1. 1차원 배열
int[] kor = new int[3];//4바이트*3 -> 12바이트 할당
kor[0] = 10;
kor[1] = 30;
kor[2] = 50;
System.out.println(kor[0]);
System.out.println(kor[1]);
System.out.println(kor[2]);
//System.out.println(kor[3]);
//예외, ArrayIndexOutOfBoundsException
//kor의 3번 요소는 존재하지 않음
System.out.println(kor.length);//3, 배열 kor이 가진 요소 수
////////////////////////////////////
//메모리 할당 및 초기값 지정
int[] eng = {20, 30, 40}; //자동으로 배열 요소 3개로 됨
//자바스크립트에서는 [20, 30, 40]으로 사용
for(int i = 0; i < eng.length; i++) {
System.out.println(eng[i]);
}//for end
double[] aver = {1.2, 3.4, 5.6};
for(int i = 0; i < aver.length; i++) {
System.out.println(aver[i]);
}//for end
char[] ch = {'h','e','l','l','o'};
for(int i = 0; i < ch.length; i++) {
System.out.println(ch[i]);
}//for end
String[] name = {"개나리", "진달래", "무궁화"};
for(int i = 0; i < name.length; i++) {
System.out.println(name[i]);
}//for end
////////////////////////////////////////
//2. 2차원 배열
//-> [행][열]
//-> 행, 줄, row
//2행 3열
int[][] mat = new int[2][3]; //4바이트 * 6 -> 24바이트 할당
mat[0][0] = 10;
mat[0][1] = 20;
mat[0][2] = 30;
mat[1][0] = 40;
mat[1][1] = 50;
mat[1][2] = 60;
for(int r = 0; r <= 1 ; r++) {
for(int c = 0; c <= 2; c++) {
System.out.println(mat[r][c]);
}//for end
}//for end
System.out.println("------------------");
//System.out.println(mat[4][4]);
//예외, ArrayIndexOutOfBoundsException
//mat의 4행 4열은 존재하지 않음
System.out.println(mat.length);//2, 행개수
System.out.println(mat[0].length);//3, 0행의 열개수
System.out.println(mat[1].length);//3, 1행의 열개수
//자바에서는 2차원 배열의 행마다 열의 개수가 달라도 상관없음
int[][] music = {
{10, 20} //0행의 요소
,{30, 40, 50, 60} //1행의 요소
,{70, 80, 90} //2행의 요소
};
System.out.println(music.length); //3
System.out.println(music[0].length); //2
System.out.println(music[1].length); //4
System.out.println(music[2].length); //3
위의 배열 출력
int row = music.length;//3
for(int r = 0; r < row; r++) {
int col = music[r].length;
for(int c = 0; c < col; c++) {
System.out.print(music[r][c]+" ");
}
System.out.println();
}
/*
10 20
30 40 50 60
70 80 90
*/
/////////////////////////////////////////////
//배열 연습문제
char[] ch = {'I', 't', 'W', 'i', 'l', 'l'};
int size = ch.length; // 6
//문1) 대, 소문자의 개수 각각 구하시오
//->대문자 : 2개
//->소문자 : 4개
/* 모범답안
int upper = 0;
int lower = 0;
for(int a = 0; a < size; a++) {
if( ch[a]>='A' && ch[a] <= 'Z') {upper++;}
if( ch[a]>='a' && ch[a] <= 'z') {lower++;}
}//for end
*/
//내가한것, 성공
int upper = 0;
int lower = 0;
for(int a = 0; a < size; a++) {
if( ch[a] <= 'Z') {
upper++;
} else {
lower++;
}
}
System.out.println("대문자 : " + upper + "개");
System.out.println("소문자 : " + lower + "개");
System.out.println("----------------------------");
//문2) 대소문자 서로 바꿔 출력
//->iTwILL
/* 모범답안
for(int a = 0; a < size; a++) {
if( ch[a]>='A' && ch[a] <= 'Z' )
{sysout.printf("%c", ch[a]+32);} //if end
if( ch[a]>='a' && ch[a] <= 'z' )
{sysout.printf("%c", ch[a]-32);} //if end
}//for end
*/
//내가한것, 성공
for(int a = 0; a < size; a++) {
if( ch[a] <= 'Z') {
ch[a] = (char)((int)ch[a] + 32);
} else {
ch[a] = (char)((int)ch[a] - 32);
}
System.out.print(ch[a]);
}
System.out.println();
System.out.println("----------------------------");
/////////////////////////////////////
//문3) 모음의 개수를 구하시오 aeiou
//-> 모음의 개수 : 2개
//모범답안
int mo = 0;
for(int i = 0; i < size; i++) {
char c = ch[i];
if(c >= 'A' && c <= 'Z') { //대문자인가?
c =(char)(c+32); //소문자로 변경
}//if end
switch(c) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' : mo++;
}//switch end
}//for end
System.out.printf("\n 모음의 개수: %d \n" , mo);
/*
모두 소문자로 바꾸는
for(int a = 0; a < size; a++) {
if( ch[a] <= 'Z') {
ch[a] = (char)((int)ch[a]+32);
}//if end
System.out.println(ch[a]);
}
*/
//내가한것, 성공
int count = 0;
for(int b = 0; b < size; b++) {
if(ch[b] == 'i'||ch[b] == 'I') {
count++;
}
}
System.out.println("모음의 개수 : " + count);
/*
for(int b = 0; b < size; b++) {
if(ch[b]=='a' || ch[b]=='e' || ch[b]=='i'||ch[b]=='o'||ch[b]=='u' ) {
count++;
}
}
*/
System.out.println("----------------------------");
//////////////////////////
//문4) 각 행의 모음의 개수를 구하시오
//str[0]행 : 2개
//str[1]행 : 1개
//str[2]행 : 2개
char[][] str = {
{'Y','e','a','r'}
,{'M','o','n','t','h'}
,{'D','a','t','e'}
};
//모범답안
int row = str.length; //3
int hap = 0;
for(int r = 0; r < row; r++) {
int col = str[r].length;
for(int c = 0; c < col; c++) {
char w = str[r][c];
if(w >= 'A' && w <= 'Z') { //대문자인가?
w =(char)(w+32); //소문자로 변경
}//if end
switch(w) {
case 'a' :
case 'e' :
case 'i' :
case 'o' :
case 'u' : count++;
}//switch end
}//for end
System.out.printf("\nstr[%d]행 모음의 개수 : %d개", r, count);
count = 0;//행별 모음개수 구하기 위해 매 행마다 count 초기화
}//for end
System.out.println();
System.out.println("----------------------------");
//내가한것, 성공
int strsiz = str.length;
int count1 = 0;
for(int a = 0; a < strsiz; a++) {
int col = str[a].length;
for(int b = 0; b < col ; b++) {
if(str[a][b] == 'a'||str[a][b] == 'e'
||str[a][b] == 'i'||str[a][b] == 'o'||str[a][b] == 'u'
||str[a][b] == 'A'||str[a][b] == 'E'||str[a][b] == 'I'
||str[a][b] == 'O'||str[a][b] == 'U') {
count1++;
}//if end
}//b for end
System.out.println("str[" + a + "]행 : " + count1 + "개");
count1 = 0;
}//a for end
System.out.println("----------------------------");
///////////////////////////////////////
//문5) 대각선 방향의 각 요소의 합을 구하시오
// 대각선 ↘ 방향 합 (4+9+7) = 20
// 대각선 ↙ (2+9+6) num[0][2]+num[1][1]+num[2][0]
int[][] num = {
{4, 3, 2}
,{5, 9, 1}
,{6, 8, 7}
};
int hap1 = 0; //우하향대각선합
int hap2 = 0; //좌하향대각선합
for(int i = 0; i <= 2; i++ ) {
hap1 = hap1 + num[i][i];
hap2 = hap2 + num[i][2-i];
}
System.out.printf("\n대각선 ↘ 방향의 합 : %d", hap1);
System.out.printf("\n대각선 ↙ 방향의 합 : %d", hap2);
System.out.println();
System.out.println("----------------------------");
//내가한것 우하향만 성공
int sum = 0;
int numsiz = num.length;
for(int a = 0; a < numsiz; a++ ) {
int numcsiz = num[a].length;
for(int b = 0; b < numcsiz; b++) {
if (a == b) {
sum = sum + num[a][b];
}//if end
}//for b end
}//for a end, 우하향대각선요소합
System.out.println("↘합 : " + sum);
System.out.println("----------------------------");
// 정렬 Sort
/*
- 정렬 유형 : 오름차순, 내림차순
-정렬 방식
삽입정렬 insertion sort
선택정렬 selection sort
버블정렬 bubble sort
- selection sort 알고리즘
- 하나를 선택해 다른 값과 한번씩 비교하여 정렬(이를 반복)
9 8 7 6 5 -> 56789
9 8 7 6 5
1) 8 9 7 6 5
2) 7 9 8 6 5
3) 6 9 8 7 5
4) 5 9 8 7 6
-----------------step 1
5 8 9 7 6
5 7 9 8 6
5 6 9 8 7
-------------step 2
5 6 8 9 7
5 6 7 9 8
-------------step 3
5 6 7 8 9
-------------step 4
- bubble sort 알고리즘
9 8 7 6 5 -> 56789
8 9 7 6 5
8 7 9 6 5
8 7 6 9 5
8 7 6 5 9
-------------step 1
7 8 6 5 9
7 6 8 5 9
7 6 5 8 9
-------------step 2
6 7 5 8 9
6 5 7 8 9
-------------step 3
5 6 7 8 9
-------------step 4 폭이 점차 줄어드는 직각삼각형 모양(뒤쪽부터 정렬됨)
*/
//배열 메소드 사용
int[] su = {9,8,7,6,5};
Arrays.sort(su);
for(int idx = 0; idx < su.length; idx++) {
System.out.println(su[idx]);
}//for end
/////////////////////////////////////////////////////////
package oop0314;
public class Test06_method {
//메소드 작성 영역
//void 리턴값이 없다
public static void test1() {
System.out.println("JAVA");
}//test1() end
public static void test2() {
System.out.println("PYTHON");
return; //함수를 호출한 시점으로 되돌아간다
//마지막 return 명령어는 생략가능
}//test2() end
public static void test3(int a) {
//매개변수(parameter a)의 자료형 생략 불가
System.out.println(a);
return;
} //test3() end
public static void test4(int a, int b, int c) {
//매개변수는 개별적으로 선언한다
System.out.println(a+b+c);
return;
}//test4() end
public static void test5(double a, double b) {
System.out.println(a+b);
return;
}//test5()end
public static void test6(char b, int n) {
for(int i = 1; i <= n; i++ ) {
System.out.print(b);
}//for end
}//test6() end
public static void main(String[] args) {
// Method 메소드
// 함수, function, 프로시저
//1. 리턴값이 없는 함수
//1) 전달값(argument value)이 없는 경우
test1();//함수호출
test2();
test1();
//2) 전달값이 있는 경우
test3(10);
test4(20, 30, 40);
test5(1.2, 3.4);
//문제) #기호를 100번 출력
byte num = 100;
char ch = '#';
test6(ch, num);
}//main() end
}//class end