2017년 10월 17일 화요일

JAVA 난수의 합 계산 및 메소드

난수 발생 함수(메소드)

Math.random() : [0.0, 1.0) 임의의 실수 // 0.0은 나올 수있는데 1.0은 절대 안나옴

==> 0~99사이의 난수 발생

Math.random()*100 : [0.0, 100.0)

(int)(Math.random()*100) : [0, 99] 임의의 정수

[1, 100]
(int)(Math.random()*100) + 1

[1, 6]
(int)(Math.random()*6) + 1


import java.util.Scanner;
// LAB: 난수의 합 계산하기
public class Test002 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int i, n, sum = 0;

System.out.print("난수의 개수: ");
n = sc.nextInt();

for(i=1; i<=n; i++) {
sum += (int)(Math.random()*100);
}
System.out.println(sum);

}

}

댓글 없음:

댓글 쓰기

시스템 보안

1. 내컴퓨터는 해킹이 당한적 있는가? 있다. 2. 컴퓨터를 하게되면 해킹을 당한다. 무엇을 이용해 해킹을 하는가? 인터넷이 가장 보편적. 사회적인 공격(주변인이 사전정보를 가지고 해킹) 3. 대응을 어떻게 해야하나? 보안프로...