Goru
[Java] do -while문 본문
do- while문은 while문의 변형로 기본적인 구조는 while문과 같으나 조건식과 블럭 { }의 순서를
바꿔 놓은 것이다. 그래서 while문과 반대로 블럭{ }이 수행한 후에 조건식을 평가한다.
do -while문은 최소한 한번은 수행될 것을 보장한다.
do {
//조건식의 연산결과가 참일 떄 수행될 문장들을 적는다.
} while (조건식);
import java.util.*;
public class FlowEx28 {
public static void main(String[] args) {
int input = 0, answer = 0;
answer = (int)(Math.random() * 100) +1;
Scanner scanner = new Scanner(System.in);
do {
System.out.print("1과 100사이의 정수를 입력하세요.>");
input = scanner.nextInt();
if(input >answer) {
System.out.println("더 작은 수로 다시 시도해보세요,");
} else if(input <answer){
System.out.println("더 큰수로 다시 시도해보세요.");
}
} while(input!=answer);
System.out.println("정답입니다.");
}
}
public class FlowEx29 {
public static void main(String[] args) {
for(int i =1;i<=100;i++) {
System.out.printf("i=%d ", i );
int tmp = i;
do {
if(tmp%10%3==0 && tmp%10!=0)
System.out.print("짝");
} while((tmp/=10) !=0);
System.out.println();
}
}
}
'Java' 카테고리의 다른 글
[Java] 배열 (0) | 2021.12.29 |
---|---|
[Java] break,continue문 (0) | 2021.12.28 |
[Java] while 문 (0) | 2021.12.28 |
[Java] 반복문_2 (for) (0) | 2021.12.28 |
[Java] 반복문 (0) | 2021.12.26 |