char[] ch= {'I', 't', 'W', 'i', 'l', 'l'};
int size=ch.length; //6
문제 1
//문1)대, 소문자의 갯수를 각각 구하시오
//->대문자 : 2개
//->소문자 : 4개
int cap=0;
int sl=0;
for(int i=0; i<size; i++) {
if(ch[i]>='A' && ch[i]<='Z') {
cap+=1;
} else {
sl+=1;
}
}
System.out.println("대문자 : " + cap + "개");
System.out.println("소문자 : " + sl + "개");
// 다른 풀이
int upper=0; //대문자의 갯수
int lower=0; //소문자의 갯수
for(int i=0; i<size; i++) {
if(ch[i]>='A' && ch[i]<='Z') { upper++; }
if(ch[i]>='a' && ch[i]<='z') { lower++; }
}//for end
System.out.printf("대문자 갯수: %d\n", upper);
System.out.printf("소문자 갯수: %d\n", lower);
문제 2
//문2)대소문자를 서로 바꿔서 출력하시오
//-> iTwILL
for(int i=0; i<size; i++) {
if(ch[i]>='A' && ch[i]<='Z') {
ch[i]=(char)(ch[i]+32);
} else {
ch[i]=(char)(ch[i]-32);;
}
}
System.out.println(ch);
// 다른 풀이
for(int i=0; i<size; i++) {
if(ch[i]>='A' && ch[i]<='Z') {
System.out.printf("%c", ch[i]+32);
}//if end
if(ch[i]>='a' && ch[i]<='z') {
System.out.printf("%c", ch[i]-32);
}//if end
}//for end
System.out.println();
문제 3
//문3)모음의 갯수를 구하시오 (A E I O U a e i o u)
//-> 모음의 갯수 : 2개
int vcount = 0;
char[] vowel = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};
for(int i=0; i<size; i++) {
for (int j=0; j<vowel.length; j++) {
if(ch[i]==vowel[j]) {
vcount+=1;
}//if end
}//for end
}//for end
System.out.println("모음의 갯수 : " + vcount + "개");
// 다른 풀이
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);
문제 4
//문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 strlen = str.length;
int rowvnum = 0;
for(int a=0; a<strlen; a++) {
int b=str[a].length;
for(int c=0; c<b; c++) {
for(int d=0; d<vowel.length; d++) {
if(str[a][c]==vowel[d]) {
rowvnum+=1;
}//if end
}//for end
}//for end
System.out.println("str["+a+"]행 : " + rowvnum + "개");
rowvnum=0;
}//for end
// 다른 풀이
int row=str.length; //3
int count=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; //각 행마다 모음의 갯수를 구하기 때문에 초기화해야 함
}//for end
문제 5
//문5) 대각선 방향의 각 요소의 합을 구하시오
//대각선 ↘ 방향의 합 (4+9+7)
//대각선 ↙ 방향의 합 (2+9+6)
int[][] num= {
{4, 3, 2}
,{5, 9, 1}
,{6, 8, 7}
};
int rf = 0;
int lf = 0;
for(int a=0; a<num.length; a++) {
int b=num[a].length;
for(int c=0; c<b; c++) {
if(a==c && (a!=1 && c!=1)) {
rf=rf+num[a][c];
} else if ((a+c)==2 && (a!=1 && c!=1)) {
lf=lf+num[a][c];
} else if (a==1 && c==1) {
rf=rf+num[1][1];
lf=lf+num[1][1];
}//if end
}//for end
}//for end
System.out.println(rf);
System.out.println(lf);
// 다른 풀이
//대각선 ↘ 방향의 합 (4+9+7) num[0][0]+num[1][1]+num[2][2]
//대각선 ↙ 방향의 합 (2+9+6) num[0][2]+num[1][1]+num[2][0]
int hap1=0; //대각선 ↘
int hap2=0; //대각선 ↙
for(int i=0; i<num.length; i++) {
hap1=hap1+num[i][i];
hap2=hap2+num[i][(num.length-1)-i];
}//for end
System.out.printf("\n대각선 ↘ 방향의 합 : %d", hap1);
System.out.printf("\n대각선 ↙ 방향의 합 : %d", hap2);
'웹개발 교육 > Java' 카테고리의 다른 글
[31일] Java (15) - Method (0) | 2022.09.07 |
---|---|
[30 ~ 31일] Java (14) - 정렬 (0) | 2022.09.06 |
[30일] Java (12) - 배열 (0) | 2022.09.06 |
[30일] Java (11) - 연습 문제 (0) | 2022.09.06 |
[30일] Java (10) - 반복문 (0) | 2022.09.06 |