07. JAVA, 제어문 (반복문, 조건문, for, while, if, switch) & 연습문제
○ 조건문 (if, switch case)
- 개념(↓링크 참고)은 Java Script와 비슷하기 때문에 문제풀면서 연습
08.JavaScript 조건문(if, switch~case)
○ 제어문 - 프로그램의 흐름을 제어 - 조건문: if문, switch~case문 - 반복문: for문, while문, do~while문 - break문, contunue문 ○ if 조건문 형식 ① if(조건) {조건이 True이면 처리;} ② if(조..
binscode.tistory.com
Q1) 임의의 문자가 대문자, 소문자, 기타인지 구분해서 출력하시오
char ch='d';
if(ch>='A' && ch<='Z') {
System.out.printf("%c 대문자\n", ch);
}else if(ch>='a' && ch<='z') {
System.out.printf("%c 소문자\n", ch);
}else {
System.out.printf("%c 기타\n", ch);
}
Q2) 대문자는 소문자로, 소문자는 대문자로 서로 바꿔서 출력하시오.
나머지 기호는 그대로 출력 (아스키코드 참고)
if(ch>='A' && ch<='Z') {
ch=(char)(ch+32);
System.out.println(ch);
}else if(ch>='a' && ch<='z') {
ch=(char)(ch-32);
System.out.println(ch);
}else {
System.out.println(ch);
}
Q3) 평균 점수에 따라서 A, B, C, D, F 학점을 출력하시오(switch문 활용)
int kor=100, eng=100, mat=100;
int aver=(kor+eng+mat)/3;
switch(aver/10){
case 10 :
case 9 : System.out.println("A"); break;
case 8 : System.out.println("B"); break;
case 7 : System.out.println("C"); break;
case 6 : System.out.println("D"); break;
default : System.out.println("F"); break;
} //switch end
○ 반복문 (if, switch case)
- 자세한 개념(↓링크 참고)은 Java Script와 비슷하기 때문에 예제로 복습 후 연습문제
09. JavaScript 반복문(for, while, do~while, break, continue)
○ 반복문 - for문, while문, do~while문 - break문 - continue문 - 무한 Loop : 끝이 없는 반복 ○ for 반복문 형식 ① for(시작값; 종료값; 증감){반복하고자 하는 명령어들;} ② for( in ) {} ③ for( : )..
binscode.tistory.com
① for문
for(int a=1; a<=3; a++) {
System.out.println("JAVA");
}//for end
//참고
//System.out.println(a); 에러(for문에 선언된 변수는 for문에서만 사용가능)
int b=0;
for(b=1; b<=3; b++) {
System.out.println("PYTHON");
}//for end
System.out.println(b); //4 : b가 for문 밖에서 정의된 뒤 쓰였기 때문에 밖에서도 사용가능
② while문
int i=0;
while (i<=3) {
System.out.println("SEOUL");
i++;
}//while문
③ do~while문
int j=0;
do {
System.out.println("JEJU");
j++;
}while(j<=3);
④ break와 continue
for(int a=1; a<10; a++) {
if(a==5) {
break;
}//if end
System.out.print(a + " "); //결과 : 1 2 3 4
}//for end
for(int a=1; a<10; a++) {
if(a==5) {
continue;
}//if end
System.out.print(a + " "); //결과 : 1 2 3 4 6 7 8 9
}//for end
● Practice 연습문제
Q1) 알파벳을 아래와 같이 출력하시오
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
Q2) 아래와 같이 출력하시오
//방법1
for(a=1; a<=4; a++) {
for(int b=1; b<=4; b++) {
if(a<=b) {
System.out.print("★");
}else {
System.out.print(" ");
}
}//for end
System.out.println();
}//for end
//방법2
for(int i=0; i<4; i++) {
for(int j=0; j<i; j++) {
System.out.print(" ");
}
for(int k=4; k>i; k--) {
System.out.print("★");
}
System.out.println();
}//for end
//방법3
for(int i=1; i<=4; i++) {
for(int j=1; j<=4; j++) {
if(i<=j) { // 행렬 생각하면 됨!
System.out.print("★");
}else {
System.out.print(" ");
}//if end
}//for end
System.out.println();
}//for end
Q3) 다음식의 결과를 구하시오
1 - 2 + 3 - 4 + 5 ... 100 =
int hap=0;
boolean flag=false;
for(a=1; a<=100; a++) {
if(flag) {
hap=hap-a;
flag=false;
}else {
hap=hap+a;
flag=true;
}//if end
}//for end
System.out.println(hap);
Q4) 아래와 같이 계산해서 출력하시오
int sum1=0;
for(a=10; a<=100; a=a+10) {
for(int b=a-9; b<=a; b++) {
sum1=sum1+b;
}//for end
System.out.println((a-9) + "+...+" + a + "=" + sum1);
sum1=0;
}//for end
Q5) 2g, 3g, 5g짜리 추가 각각 5개씩 있을 때,
세 가지 추의 조합으로 무게가 40~45 사이일 때
각 추의 개수와 무게를 출력하는 프로그램
System.out.println("2g 3g 5g total");
int total=0;
for(a=1; a<=5; a++) {
for(int b=1; b<=5; b++) {
for(int c=1; c<=5; c++) {
total=(a*2)+(b*3)+(c*5);
if(total>=40 && total<=45) {
System.out.printf(" %d %d %d = %d\n", a, b, c, total);
}
}//for end
}//for end
}//for end
Q6) 3의 배수의 누적의 합이 100이 넘어가려면 3부터 어디까지 더해야 하는지 구하시오
3+6+9+12+...+24 = 108
int su=0;
int hap2=0;
String str="";
while(true) {
su=su+3;
hap2=hap2+su;
str=str+su+"+";
if(hap2>100) {
break;
}//if end
}//while end
System.out.println(str + "...=" + hap2);
Q7) 어느 달팽이는 낮에는 3cm 올라가고, 밤에는 2.5cm 내려온다고 할 때,
달팽이가 13cm의 나무 꼭대기에 올라가려면 며칠이 걸리는지 구하시오
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+"일");