반응형
Math.random() - 0.0과 1.0사이의 임의의 double값을 반환
0.0 <= Math.random() < 1.0 (0.0 ~ 0.9999..)
class Ex4_7 {
public static void main(String args[]) {
int num = 0;
// 괄호{} 안의 내용을 20번 반복한다.
// Quiz. 1~10사이의 난수를 20개 출력하시오
// Quiz. -5~5사이의 난수를 20개 출력하시오
for (int i = 1; i <= 20; i++) {
// System.out.println(Math.random()); // 0.0<=x<1.0
// System.out.println(Math.random()*10); // 0.0<=x<10.0
// System.out.println((int)(Math.random()*10)); // 0<=x<10
// System.out.println((int)(Math.random()*10)+1); // 1<=x<11, 1~10
//
// System.out.println(Math.random()); // 0.0<=x<1.0
// System.out.println(Math.random()*11); // 0.0<=x<11.0
// System.out.println((int)(Math.random()*11)); // 0<=x<11
System.out.println((int)(Math.random()*11)-5); // -5<=x<6
}
}
}
반응형
'java↗' 카테고리의 다른 글
java↗5-5. 중첩 for문 (0) | 2024.03.16 |
---|---|
java↗5-4. 반복문 for문 (1) | 2024.03.15 |
java↗5-2. switch문 (0) | 2024.03.15 |
java↗5-1. if-else if문, 중첩 if문 (2) | 2024.03.15 |
java↗5. 조건문과 반복문 (0) | 2024.03.15 |