Java/이것이자바다
4장 확인문제 4번 주사위 합 while문 이용
유호야
2020. 10. 18. 11:35
반응형
package ch4;
public class Exercise04 {
public static void main(String[] args) {
// while문과 Math.random() 메소드를 이용해서 두 개의 주사위를 던졌을 때 나오는 눈을
// (눈1, 눈2) 형태로 출력하고, 눈의 합이 5가 아니면 계속 주사위를 던지고, 눈의 합이 5이면 실행을 멈추는 코드를 작성해보세요.
// 눈의 합이 5가 되는 조합은 (1,4), (4, 1), (2, 3), (3,2) 입니다.
boolean run = true;
for(int i = 0; i<=10; i++) {
while (run) {
int dice1 = (int)(Math.random()*6) + 1;
int dice2 = (int)(Math.random()*6) + 1;
if (dice1 + dice2 == 5) {
System.out.println("(" + dice1 + ", " + dice2 + ")");
run = false;
}
}
}
}
}
반응형