영상
문제
<script>
/*
[문제]
철수는 주사위 2개를 가지고 있다.
주사위는 눈금이 1~6까지 있다.
철수가 주사위 2개를 던졌을 때 그 합을 출력하시오.
단, 주사위 눈금이 서로 같으면 6을 추가로 더하시오.
[예]
1, 2 ==> 3
1, 1 ==> 2 + 6
*/
</script>
Java
복사
해설
<script>
/*
[문제]
철수는 주사위 2개를 가지고 있다.
주사위는 눈금이 1~6까지 있다.
철수가 주사위 2개를 던졌을 때 그 합을 출력하시오.
단, 주사위 눈금이 서로 같으면 6을 추가로 더하시오.
[예]
1, 2 ==> 3
1, 1 ==> 2 + 6
*/
let dice1 = Math.floor(Math.random() * 6) + 1; // [0 ~ 5] + 1
let dice2 = Math.floor(Math.random() * 6) + 1; // [0 ~ 5] + 1
console.log(dice1 + ", " + dice2);
document.write(dice1 + ", " + dice2 + "<br>");
let total = dice1 + dice2;
if(dice1 == dice2) {
console.log("서로 같다!");
document.write("서로 같다!<br>");
total = total + 6;
}
console.log("합 = " + total);
document.write("합 = " + total);
</script>
Java
복사