반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- google apps script
- dataframe
- Github
- GIT
- PySpark
- 파이썬
- Google Excel
- django
- matplotlib
- list
- Google Spreadsheet
- PostgreSQL
- Redshift
- numpy
- Presto
- SQL
- Tkinter
- gas
- Kotlin
- Java
- PANDAS
- string
- math
- array
- Apache
- hive
- c#
- Excel
- Python
Archives
- Today
- Total
달나라 노트
Java - Math : max, min, sqrt, abs, random 본문
728x90
반응형
Original source = www.w3schools.com
이번에는 여러 숫자 관련 기능들을 알아봅시다.
max method는 적힌 2개의 arguments 중 더 큰 값을 반환합니다.
public class JavaMath {
public static void main(String args[]) {
System.out.println(Math.max(1, 2));
}
}
-- Result
2
min method는 적힌 2개의 arguments 중 더 작은 값을 반환합니다.
public class JavaMath {
public static void main(String args[]) {
System.out.println(Math.min(1, 2));
}
}
-- Result
1
sqrt는 주어진 argument의 제곱근을 반환합니다.
public class JavaMath {
public static void main(String args[]) {
System.out.println(Math.sqrt(100));
}
}
-- Result
10.0
abs는 절대값을 반환합니다.
public class JavaMath {
public static void main(String args[]) {
System.out.println(Math.abs(-102));
}
}
-- Result
102
random은 0 이상이며 1 미만인 숫자를 무작위로 반환합니다.
public class JavaMath {
public static void main(String args[]) {
System.out.println(Math.random());
}
}
-- Result
0.6880629917991365
random method를 응용하면 아래처럼 어느 범위의 정수를 랜덤하게 받아올 수 있습니다.
아래 예시는 0 이상이며 101 미만인 정수를 랜덤하게 반환합니다.
public class JavaMath {
public static void main(String args[]) {
int int_random = (int) (Math.random() * 101);
System.out.println(int_random);
}
}
-- Result
56
728x90
반응형
'Java' 카테고리의 다른 글
| Java - while loop (0) | 2021.03.11 |
|---|---|
| Java - switch ~ case ~ default (0) | 2021.03.11 |
| Java - if ~ else if ~ else (0) | 2021.03.11 |
| Java - For loop : For 반복문 (0) | 2021.03.11 |
| Java - break, continue (0) | 2021.03.11 |
Comments