Search

Math

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
๋ณต์‚ฌ