do while 공부중이다!
먼저 for문을 do while로 변경하는거로 시작했다.
int total=0;
/*
for(int i=1; i<=100; i++) {
total += i;
}
//for를 while로 바꾸기
int i=1 를 여기로
for을 while로 바꾸기(int i=1; i<=100; i++) {
total += i;
i++ 을 여기로
}
*/
int i=1;
while( i<=100) {
total += i;
i++;
}
System.out.println("1부터 100까지의 합: "+total);
생각보다 do while로 바꾸는건 어렵지 않았다!
int total=0;
/*
for(int i=1; i<=100; i++) {
total += i;
}
//for를 while로 바꾸기
int i=1 를 여기로
for을 while로 바꾸기(int i=1; i<=100; i++) {
total += i;
i++ 을 여기로
}
*/
int i=1;
do {
total += i;
i++;
}
while( i<=100);
System.out.println("1부터 100까지의 합: "+total);
//아래의 중첩for문을 중첩 while문으로 변경하는 코드를 작성하시오.
for(int i=0; i<=9; i++) { //0줄부터 9줄까지~ 곱하는 수 변경 반복문
for(int j=2; j<=9; j++) { //단수 변경 반복문
if(i==0) {
System.out.printf(" [%d 단]\t\t",j);
}else {
System.out.printf("%d x %d = %d\t",j,i,i*j);
}
}
System.out.println();
위에 for문을 중첩 while문으로 변경!
int num=0;
while(num<=9) {
int dan =2;
while(dan<=9) {
if(num==0) {
System.out.printf(" [%d 단]\t\t",dan);
}else {
System.out.printf("%d x %d = %d\t",dan,num,dan*num);
}
dan++;
}
num++;
System.out.println();
}
차근차근 하나씩 바꿔 나가니 될줄 알았는데 안됐었다
dan++ 랑 num++ 증감식을 while바깥으로 빼줬어야 했다!
//중첩 do while문
/*
for(int i=0; i<=9; i++) { //0줄부터 9줄까지~ 곱하는 수 변경 반복문
for(int j=2; j<=9; j++) { //단수 변경 반복문
if(i==0) {
System.out.printf(" [%d 단]\t\t",j);
}else {
System.out.printf("%d x %d = %d\t",j,i,ij);
}
}
System.out.println();
}
/
배워와서 쓰겠다! 아무리해도 자꾸 오류가 난다!
do{
조건문이 true일때 조건식
}
while(조건문)
참고문헌- 자바의정석 (남궁성)