영상
개념
<script>
/*
[문제]
2~5 사이의 숫자를 랜덤으로 저장하고,
랜덤 숫자의 개수만큼 숫자를 더하는 문제와 답을 만들어 출력하시오.
단, 더하기 할 숫자들은 1~9사이의 랜덤 숫자이어야 한다.
아래 [출력] 뒤에 나오는 모양과 똑같은 모양으로 출력한다.
(단, 숫자는 랜덤이므로 숫자는 다르게 나올 수 있다.)
[예시1]
랜덤 ==> 3
[출력] 5 + 3 + 2 = 10
[예시2]
랜덤 ==> 5
[출력] 6 + 5 + 2 + 7 + 8 = 28
*/
let count = Math.floor(Math.random() * 4) + 2; // [0 ~ 3] + 2
console.log("반복횟수 = " + count);
document.write("반복횟수 = " + count + "<br>");
let total = 0;
let i = 0;
while(i < count) {
let num = Math.floor(Math.random() * 9) + 1; // [0 ~ 8] + 1
document.write(num);
if(i < count - 1) {
document.write(" + ");
}
total += num;
i += 1;
}
console.log(total);
document.write(" = " + total);
</script>
Java
복사