Application Programming Interface(API)λ μμ©νλ‘κ·Έλ¨μμ μ¬μ©ν μ μλλ‘,
νΉμ κΈ°λ₯μ μ¬μ©ν μ μλλ‘ μ 곡λλ λ©μλ, ν΄λμ€, μΈν°νμ΄μ€ λ±μ μ§ν©μ μλ―Ένλ€.
Javaμμ APIλ Java Standard Libraryμ ν¬ν¨λ ν΄λμ€ λ° μΈν°νμ΄μ€μ λͺ¨μμ λ»νλ©°, java.lang, java.util, java.io λ±μ ν¨ν€μ§μ ν¬ν¨λ λ€μν κΈ°λ₯μ μ 곡νλ€.
μν κ΄λ ¨ μ°μ°μ μ 곡νλ Javaμ λ΄μ₯ ν΄λμ€
λͺ¨λ λ©μλλ static λ©μλ β κ°μ²΄ μμ± μμ΄ μ¬μ© κ°λ₯
java.lang ν¨ν€μ§μ ν¬ν¨λμ΄ μμ΄ import μμ΄ μ¬μ© κ°λ₯
| λ©μλ | μ€λͺ | μμ |
|---|---|---|
Math.abs(x) | μ λκ° λ°ν | Math.abs(-10) // 10 |
Math.pow(x, y) | x^y (κ±°λμ κ³±) λ°ν | Math.pow(2, 3) // 8.0 |
Math.sqrt(x) | μ κ³±κ·Ό λ°ν | Math.sqrt(25) // 5.0 |
Math.max(a, b) | λ κ° μ€ μ΅λκ° λ°ν | Math.max(3, 7) // 7 |
Math.min(a, b) | λ κ° μ€ μ΅μκ° λ°ν | Math.min(3, 7) // 3 |
Math.round(x) | λ°μ¬λ¦Ό (long λ°ν) | Math.round(4.6) // 5 |
Math.ceil(x) | μ¬λ¦Ό (double λ°ν) | Math.ceil(4.2) // 5.0 |
Math.floor(x) | λ΄λ¦Ό (double λ°ν) | Math.floor(4.8) // 4.0 |
Math.random() | 0.0 μ΄μ 1.0 λ―Έλ§μ λμ λ°ν | Math.random() * 10 // 0~9 μ¬μ΄ λμ |
(int) (Math.random * ꡬνλ €λ λμμ κ°―μ) + ꡬνλ €λ λμμ μ΅μκ°
public static void main(String[] args) {
/* λμμ νμ© */
/* Math.random()μ μ΄μ©ν΄ λ°μν λμλ 0λΆν° 1μ κΉμ§μ μ€μ λ²μμ λμκ°μ λ°ννλ€.
* μνλ λ²μμ λμλ₯Ό ꡬνλ 곡μ
*(int) (Math.random * ꡬνλ €λ λμμ κ°―μ) + ꡬνλ €λ λμμ μ΅μκ°
*/
/* 1 ~ 10κΉμ§μ λμ λ°μ */
int random2 = (int) (Math.random() * 10) + 1;
System.out.println("1 λΆν° 10 μ¬μ΄μ λμ : " + random2);
/* -128 ~ 127κΉμ§μ λμ λ°μ */
//int random4 = (int) (Math.random() * 256) + (-128);
int random4 = (int) (Math.random() * 256) - 128;
System.out.println("-128 λΆν° 127κΉμ§μ λμ λ°μ : " + random4);
}
random.nextInt(ꡬνλ €λ λμμ κ°―μ) + ꡬνλ €λ λμμ μ΅μκ°
public static void main(String[] args) {
/* java.util.Random ν΄λμ€ */
/* java.util.Random ν΄λμ€μ nextInt() λ©μλλ₯Ό μ΄μ©ν λμ λ°μ
* nextInt(int bound) : 0λΆν° λ§€κ°λ³μλ‘ μ λ¬λ°μ μ μ λ²μκΉμ§μ λμλ₯Ό λ°μμμΌμ μ μ ννλ‘ λ°ν */
/* μνλ λ²μμ λμλ₯Ό ꡬνλ 곡μ
* random.nextInt(ꡬνλ €λ λμμ κ°―μ) + ꡬνλ €λ λμμ μ΅μκ°
* */
Random random = new Random();
/* 1λΆν° 10κΉμ§ λμ λ°μ */
int randomNumber2 = random.nextInt(10) + 1;
System.out.println("1 λΆν° 10 κΉμ§μ λμ : " + randomNumber2);
/* -128 λΆν° 127κΉμ§μ λμ λ°μ */
//int randomNumber4 = random.nextInt(256) - 128;
int randomNumber4 = new Random().nextInt(256) - 128; //κ°μ²΄λ₯Ό μμ±νμλ§μ λ°λ‘ λ©μλ νΈμΆλ ν μ μλ€.
System.out.println("-128 λΆν° 127 κΉμ§μ λμ : " + randomNumber4);
}
μ¬μ©μ μ λ ₯μ μ²λ¦¬νλ ν΄λμ€
ν€λ³΄λ, νμΌ, λ¬Έμμ΄ λ±μμ μ λ ₯μ λ°μ μ μμ
java.util ν¨ν€μ§μ ν¬ν¨λμ΄ μμ΄ import νμ
κ°μ²΄ μμ±: Scanner sc = new Scanner(System.in);
μ
λ ₯ λ°κΈ°: sc.nextInt(), sc.nextLine(), sc.nextDouble() λ±
μ¬μ© ν λ«κΈ°: sc.close(); (μμ λμ λ°©μ§)
| λ©μλ | μ€λͺ | μμ |
|---|---|---|
nextInt() | μ μ μ λ ₯ | sc.nextInt(); |
nextDouble() | μ€μ μ λ ₯ | sc.nextDouble(); |
nextLine() | ν μ€(곡백 ν¬ν¨) μ λ ₯ | sc.nextLine(); |
next() | 곡백 μ κΉμ§ λ¬Έμμ΄ μ λ ₯ | sc.next(); |
hasNextInt() | μ μ μ λ ₯ μ¬λΆ νμΈ | sc.hasNextInt(); |
close() | Scanner λ«κΈ° (λ©λͺ¨λ¦¬ ν΄μ ) | sc.close(); |
public static void main(String[] args) {
/* μ€μΊλ μ£Όμ μ¬ν */
/* μ€μΊλμ next λ©μλλ€μ μ
λ ₯ν λ΄μ©μ λ²νΌλ‘λΆν° ν ν°λ¨μλ‘ λΆλ¦¬ν΄μ μ½μ΄μ¨λ€.
* κ·Έλμ ν¬κ² λ κ°μ§ μ¬νμ μ£Όμν΄μΌ νλ€.
* 1. next()λ‘ λ¬Έμμ΄ μ
λ ₯ λ°μ ν μ μ, μ€μ, λ
Όλ¦¬κ° μ
λ ₯ λ°μ λ
* 2. μ μ, μ€μ, λ
Όλ¦¬κ° μ
λ ₯ ν next()λ‘ λ¬Έμμ΄ μ
λ ₯λ°μ λ
* */
/* μ€μΊλ κ°μ²΄ μμ± */
Scanner sc = new Scanner(System.in);
/* 1. next()λ‘ λ¬Έμμ΄ μ
λ ₯ λ°μ ν μ μ, μ€μ, λ
Όλ¦¬κ° μ
λ ₯ λ°μ λ */
System.out.print("λ¬Έμμ΄μ μ
λ ₯ν΄μ£ΌμΈμ : ");
String str1 = sc.next(); //곡백μ΄λ κ°νλ¬Έμ μ κΉμ§λ₯Ό μ½μ΄μ¨λ€.
System.out.println("str1 : " + str1);
System.out.print("μ«μλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ : ");
int num1 = sc.nextInt(); //μ μ κ°μ μ½μ΄μ¨λ€.
System.out.println("num1 : " + num1);
}
"μλ
νμΈμ λ°κ°μ΅λλ€" μ κ°μ΄ κ³΅λ°±μ΄ μμ κ²½μ° λκ°μ "μλ
νμΈμ" μ "λ°κ°μ΅λλ€" λ‘ λΆλ¦¬κ° λλ€.
next()λ "μλ
νμΈμ"λ§ μ½κΈ° λλ¬Έμ nextInt()κ° μ μλ‘ μ½μ§ λͺ»ν΄ μ€λ₯κ° λ°μνλ€.
κ³΅λ°±μ΄ μλ λ¬Έμμ΄μ λ°μκ±°λ©΄ nextLine()μ μ΄μ©νμ.
νΉμ μ
λ ₯ λ°μ λ²νΌλ₯Ό μ€ λ¨μλ‘ ν ν°μ λΆλ¦¬νκΈ° λλ¬Έμ ν΄λΉ λΌμΈμ ν ν°μ λ€ μ½κ³ λ€μ μ€λ‘ μ΄λμν€κ³ μΆμ λλ
ν λΌμΈμ λͺ¨λ ν ν°μ μ½μ΄μ€λ sc.nextLine(); νΈμΆμ μ€κ°μ ν λ² λ£μ΄μ€λ ν΄κ²°μ λλ€.
μμμ λ¨κΈ΄ κ°νμ nextLine()μ΄ μ½κ³ λμ΄κ°μ μ
λ ₯μ νλ‘κ·Έλ¨μ΄ μ’
λ£λλ€.
ν΄κ²°νλ λ°©λ²μ κ°νμ λ°μμ€ nextLine()μ ν μ€ λ λͺ
μν΄μ€μΌλ‘ λ²νΌλ₯Ό λΉμ°λ©΄ λλ€.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("λ€μ μ«μλ₯Ό μ
λ ₯ν΄μ£ΌμΈμ : ");
int num2 = sc.nextInt();
System.out.println("num2 : " + num2);
System.out.print("κ³΅λ°±μ΄ μλ λ¬Έμμ΄μ νλ μ
λ ₯ν΄μ£ΌμΈμ : ");
sc.nextLine(); // μ΄ λΆλΆμ μΆκ°ν΄μ€λ€.
String str2 = sc.nextLine();
System.out.println("str2 : " + str2);
}