알고리즘 문제 풀이/Power JAVA
p182) 주사위 게임
kakk789
2022. 4. 4. 20:57
주사위의 합이 2가 되면 탈출하고 2가 나오는데 걸린 횟수를 출력해라
--- 출력 값 ---
a1> 1
a2> 1
count 5
class Dice
class Dice{
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Dice() {
value=0;
}
}
Main
Dice dice = new Dice();
Random r = new Random();
int n = 0;
int cnt=1;
int a1=0;
int a2=0;
while(true) {
n = r.nextInt(6)+1;
dice.setValue(n);
a1=dice.getValue();
dice.setValue(n);
a2=dice.getValue();
if(a1+a2 ==2) {
break;
}
cnt++;
}
System.out.println("a1> "+a1);
System.out.println("a2> "+a2);
System.out.println("count "+cnt);
반응형