Math
-
자바/java Math.abs(); 절대 값 활용(다이아 만들기)java공부 2021. 7. 25. 16:31
System.out.println(Math.abs(-10)); // 10 // 응용 *로 다이아모양 만들기 for(int i = 0; i < 5; i++) { for(int j = 0; j < Math.abs(2 - i); j++) { System.out.print(" "); } for(int j = 0; j < -2 * (Math.abs(i - 2) + 5); j++) { System.out.print("*"); } System.out.println(); } /* * * * *** * ***** * *** * * */ Math.abs(값); 값이 절대 값으로 바뀐다. 음수가 양수로 바뀜.
-
자바/java Random 난수 메소드 이용과 Math이용java공부 2021. 7. 20. 00:31
java.util.Random import java.util.Random Random r = new Random(); //int num = rnd.nextInt(); // 범위를 지정하지 않으면, 정수범위내에서 난수 추출 //int num = rnd.nextInt(10); // 경우의 수 - 10가지(0 ~ 9) //int num = rnd.nextInt(10) + 1; // 1 ~ 10 int num = rnd.nextInt(20) + 11; // 11 ~ 30 double n = rnd.nextDouble(); // 0.0 이상 1.0 미만의 실수를 리턴 java.lang.Math.random() double n = Math.random(); // 0.0 이상 1.0 미만의 실수를 리턴 //int n..