import java.util.Random;
public class Main {
public static void main(String[] args) {
Random rand=new Random();
int num=rand.nextInt(90)+10; //10부터 99까지의 랜덤정수가 있다
System.out.println(num);
int res=num;
int cnt=0;
while(num>0) {// 검사하려는 숫자가 0이 될때까지
if(num%10==3||num%10==6||num%10==9) {
cnt++;
}
if(cnt==2) {
System.out.println("짝짝");
}
else if(cnt==1) {
System.out.println("짝");
}
else {
System.out.println(res);
}
}
}
}
While문과 if ~else문을 사용
반복횟수를 알고 있기 때문에 for문을 써야 했지만, While문 연습을 위해 While문으로 작성!
While - 반복해라
If- ~이런 조건하에
아니면 while문을 탈출해라(break!)
'JAVA' 카테고리의 다른 글
배열문제 ① (0) | 2025.01.02 |
---|---|
IF~Else <홀수와 짝수 구하기> (0) | 2025.01.02 |
While문을 이용한 랜덤뽑기 (0) | 2024.12.31 |
WHILE문- 무한루프의 늪에서 벗어나고 싶다 (1) | 2024.12.30 |
자바의 조건문 (If~else 구문) (0) | 2024.12.26 |