public class MathEx {
public static void main(String[] args) {
System.out.println(Math.PI); // 3.141592653589793 (์์ฃผ์จ)
System.out.println(Math.E); // 2.718281828459045 (์์ฐ์์)
System.out.println(Math.ceil(3.125)); // ์ฌ๋ฆผ : ํด๋น ์๋ณด๋ค ํฐ ์ ์ ์ค ๊ฐ์ฅ ์์ ์
System.out.println(Math.floor(3.75)); // ๋ด๋ฆผ : ํด๋น ์๋ณด๋ค ์์ ์ ์ ์ค ๊ฐ์ฅ ํฐ ์
System.out.println(Math.sqrt(9)); // ์ ๊ณฑ๊ทผ
System.out.println(Math.pow(3, 2)); // 3์ 2์ ๊ณฑ
System.out.println(Math.exp(2)); // e์ 2์น
System.out.println(Math.round(3.14)); // ๋ฐ์ฌ๋ฆผ
System.out.println("----------------------");
// ๋ก๋ ๋ฒํธ
// Math.random() : 0.0 ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ 1.0 ๋ณด๋ค ์์ ์์์ ์ค์
// Math.random() : 0.0xxx~0.9xxx
// (0.0xxx~0.9xxx) * 10 : 0.xxx~9.xxx (0 ~ 9) - 10๊ฐ
// (0.0xxx~0.9xxx) * 20 : 0.xxx~19.xxx (0 ~ 19) - 20๊ฐ
// (0.0xxx~0.9xxx) * 45 : 0.xxx~44.xxx (0 ~ 44) - 45๊ฐ
// (0.0xxx~0.9xxx) * 45 + 1 : 1.xxx~45.xxx (1 ~ 45)
// (int)(Math.random() * 45 + 1) : 1~45
// [๊ณต์]
// (int)(Math.random() * [๊ฐ์] + [์์์ซ์])
int lotto[] = new int[6];
for (int i = 0; i < 6; i++) {
int random = (int)(Math.random() * 45 + 1);
lotto[i] = random;
// ์ค๋ณต ์ ๊ฑฐ
for (int j = 0; j < i; j++) {
// ์ค๋ณต์ด ๋๋ ๊ฒฝ์ฐ
if( lotto[j] == random ) {
i--;
}
}
}
// ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
for (int i = 0; i < lotto.length - 1; i++) {
for (int j = i+1; j < lotto.length; j++) {
// ์ ํํ i๋ฒ ์์์ ๋น๊ตํ j๋ฒ ์์
if( lotto[i] > lotto[j] ) {
// swap
// lotto[i] ์ lotto[j] ๊ตํ
int temp = lotto[i];
lotto[i] = lotto[j];
lotto[j] = temp;
}
}
}
for (int i = 0; i < lotto.length; i++) {
System.out.print( lotto[i] + " ");
}
System.out.println();
System.out.println("-----------------------------");
// ์ต๋๊ฐ
double[] arr = {90.22, 12.45, 33.22, 88.12, 70.45};
double max = Double.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
// if( max < arr[i] )
// max = arr[i];
max = Math.max(max, arr[i]); // ๋ ์ธ์ ์ค ํฐ ๊ฐ์ ๋ฐํ
}
System.out.println("์ต๋๊ฐ max : " + max);
}
}
Java
๋ณต์ฌ