๐โ API(Application Programming Interface)๋,
์์ฉํ๋ก๊ทธ๋จ์์ ์ ์ดํ ์ ์๋๋ก, ์ด์์ฒด์ ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๊ฐ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ ์ ์ดํ ์ ์๋๋ก ๋ง๋ ์ธํฐํ์ด์ค
์ฐ๋ฆฌ๊ฐ ๊ตฌํํ ์ ์๊ฑฐ๋ ๊ตฌํํ๊ธฐ ๋ฒ๊ฑฐ๋ก์ด ๊ธฐ๋ฅ์ JDK๋ฅผ ์ค์นํ๋ฉด ์ฌ์ฉํ ์ ์๋๋ก ์ ๊ณตํด๋์ ์์ค ์ฝ๋๋ฅผ ์๋ฏธ
- ๋ชจ๋ ์ฝ๋๋ฅผ ์ธ์ธ ์ ์์ผ๋ฏ๋ก API ๋ฌธ์๋ฅผ ๋ณ๋๋ก ์ ๊ณตํด์ค
๐โ ์ํ์์ ์์ฃผ ์ฌ์ฉํ๋ ์์๋ค๊ณผ method๋ค์ ๋ฏธ๋ฆฌ ๊ตฌํํด๋์ Class
- API ๋ฌธ์์์ ํ์ธํ๋ฉด Math ํด๋์ค๋ ๋ชจ๋ static์ผ๋ก ์ ์ธ๋์ด ์์ผ๋ฏ๋ก static method ํธ์ถ ๋ฐฉ์์ผ๋ก ํธ์ถ
System.out.println("-7์ ์ ๋๊ฐ : " + (java.lang.Math.abs(-7)));
System.out.println("-7์ ์ ๋๊ฐ : " + (Math.abs(-7)));
>>> java.lang. ์๋ต๊ฐ๋ฅ
๐ Ref.
* java.lang ํจํค์ง ํ์์ ์๋ ํด๋์ค๋ import ๊ตฌ๋ฌธ์ด ๋ถํ์
์๋ ์์ฃผ ์ฐ๋ ๊ธฐ๋ฅ์ด ๋ด๊ธด ํจํค์ง์ฌ์ ์ปดํ์ผ๋ฌ๊ฐ import java.lang.*; ์ด๋ผ๋ ์ฝ๋๋ฅผ ์๋์ผ๋ก ์ถ๊ฐํด์ ์ปดํ์ผํจ
System.out.println("10๊ณผ 20 ์ค ๋ ์์ ๊ฒ์ : " + Math.min(10, 20));
System.out.println("10๊ณผ 20 ์ค ๋ ํฐ ๊ฒ์ : " + Math.max(10, 20));
์ํ์ ์ผ๋ก ๋ง์ด ์ฌ์ฉํ๋ ๊ณ ์ ๋ ๊ฐ๋ค๋ ์ด๋ฏธ math์ ์ ์๋ ๊ฒ์ด ์์(ํ๋)
System.out.println("์์ฃผ์จ : " + Math.PI);
0๋ถํฐ 1์ ๊น์ง์ ์ค์ ํํ์ ๋์๋ฅผ ๋ฐ์
System.out.println("๋์ ๋ฐ์ : " + Math.random());
(int) (Math.random() * ๊ตฌํ๋ ค๋ ๋์์ ๊ฐ์) + ๊ตฌํ๋ ค๋ ๋์์ ์ต์๊ฐ
// 0 ~ 9๊น์ง์ ๋์ ๋ฐ์
int random1 = (int)(Math.random() * 10);
System.out.println("0๋ถํฐ 9์ฌ์ด์ ๋์ : " + random1);
// 1 ~ 10๊น์ง์ ๋์ ๋ฐ์
int random2 = (int)(Math.random() * 10) +1;
System.out.println("1๋ถํฐ 10์ฌ์ด์ ๋์ : " + random2);
// 10 ~ 15 ๊น์ง์ ๋์ ๋ฐ์
int random3 = (int)(Math.random() * 6) +10;
System.out.println("19๋ถํฐ 97์ฌ์ด์ ๋์ : " + random3);
// byte ํ์
์ด ๊ฐ์ง๋ ๊ฐ์ ๋ฒ์๋งํผ ๋์ ๋ฐ์ (-128 ~ 127)
int random4 = (int)(Math.random() * 256) -128;
System.out.println("byte ํ์
์ด ๊ฐ์ง๋ ๊ฐ์ ๋ฒ์ ์ฌ์ด์ ๋์ : " + random4);
// 19 ~ 97 ๊น์ง์ ๋์ ๋ฐ์ */ // (97-19)+1=79 / (max-min)+1= n
int random5 = (int)(Math.random() * 79) +19;
System.out.println("19๋ถํฐ 97์ฌ์ด์ ๋์ : " + random5);
๐โ Math.random()๊ณผ๋ ๋ ๋ค๋ฅธ Random class
java.util.Random class์ nextInt() ๋ฉ์๋๋ฅผ ์ด์ฉํ ๋์ ๋ฐ์
random.nextInt(bound)
: 0๋ถํฐ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌ๋ฐ์ ์ ์ ๋ฒ์๊น์ง์ ๋์๋ฅผ ๋ฐ์์์ผ ์ ์ ํํ๋ก ๋ณํ
import java.util.Random;
>>> java.util.Random์ ์ง์ ์ฐ๋ ๊ฒ ๋์ ์ ์ฒด๋ฅผ import
public class Application3 {
public static void main(String[] args) {
Random random = new Random();
>>> Random ๊ฐ์ฒด ์์ฑ ํ, import (shift + ctrl + O)
// 0๋ถํฐ 9๊น์ง์ ๋์ ๋ฐ์
int random1 = random.nextInt(10);
System.out.println("0๋ถํฐ 9๊น์ง์ ๋์ : " + random1);
// 1๋ถํฐ 10๊น์ง์ ๋์ ๋ฐ์
int random2 = random.nextInt(10) +1;
System.out.println("1๋ถํฐ 10๊น์ง์ ๋์ : " + random2);
// 20๋ถํฐ 45๊น์ง์ ๋์ ๋ฐ์
int random3 = random.nextInt(26) +20;
>>> (45-20)+1=26
System.out.println("20๋ถํฐ 45๊น์ง์ ๋์ : " + random3);
}
}
๐โ ์ฝ์ ํ๋ฉด์ ๊ฐ์ ์ ๋ ฅ ๋ฐ์ ์ถ๋ ฅํ ์ ์๋ Class
- ์ด๋ฐ ์ด๋ ค์ด ๊ธฐ๋ฅ ๋ํ ๋ฏธ๋ฆฌ JDK๋ฅผ ์ค์นํ๋ฉด ์์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ๊ตฌํํด๋์
import java.util.Scanner;
>>> java.util.Scanner์ ์ง์ ์ฐ๋ ๊ฒ ๋์ ์ ์ฒด๋ฅผ import
public class Application1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
>>> Scanner ๊ฐ์ฒด ์์ฑ ํ, import (shift + ctrl + O)
>>> nextLine() : ์
๋ ฅ ๋ฐ์ ๊ฐ์ ๋ฌธ์์ด๋ก ๋ฐํํด์ค
System.out.print("์ด๋ฆ์ ์
๋ ฅํ์ธ์ : ");
String name = sc.nextLine();
System.out.println("์
๋ ฅํ์ ์ด๋ฆ์ " + name + "์
๋๋ค.");
>>> nextInt() : ์
๋ ฅ ๋ฐ์ ๊ฐ์ intํ์ผ๋ก ๋ฐํํด์ค
System.out.print("๋์ด๋ฅผ ์
๋ ฅํ์ธ์ : ");
int age = sc.nextInt();
System.out.println("์
๋ ฅํ์ ๋์ด๋ " + age + "์
๋๋ค.");
>>> nextLong() : ์
๋ ฅ ๋ฐ์ ๊ฐ์ longํ์ผ๋ก ๋ฐํํด์ค
System.out.print("๊ธ์ก์ ์
๋ ฅํ์ธ์ : ");
long money = sc.nextLong();
System.out.println("์
๋ ฅํ์ ๊ธ์ก์ " + money + "์ ์
๋๋ค.");
๐ Ref.
* ์ซ์๊ฐ ์๋ ๊ฐ์ ์
๋ ฅํ๊ฒ ๋๋ฉด java.util.InputMismatchException์ด ๋ฐ์
* int ๋ฒ์๋ฅผ ์ด๊ณผํ ๊ฐ์ ์
๋ ฅํ๊ฒ ๋๋ฉด ์ญ์ InputMismatchException์ด ๋ฐ์
>>> nextFloat() : ์
๋ ฅ ๋ฐ์ ๊ฐ์ float ํ์ผ๋ก ๋ฐํํจ
System.out.print("ํค๋ฅผ ์
๋ ฅํ์ธ์ : ");
float height = sc.nextFloat();
System.out.println("์
๋ ฅํ์ ํค๋ " + height + "cm ์
๋๋ค.");
>>> nextDouble() : ์
๋ ฅ ๋ฐ์ ๊ฐ์ double ํ์ผ๋ก ๋ฐํํจ
System.out.print("์ํ๋ ์ค์๋ฅผ ์
๋ ฅํ์ธ์ : ");
double number = sc.nextDouble();
System.out.println("์
๋ ฅํ์ ์ค์๋ " + number + "์
๋๋ค.");
๐ Ref.
* ์ซ์๊ฐ ์๋ ๊ฐ์ ์
๋ ฅํ๊ฒ ๋๋ฉด java.util.InputMismatchException์ด ๋ฐ์
* ์ ์ ์
๋ ฅ ์ ์ค์๋ก ๋ณํ ํ ์ ์ ๋์ํ๋ฉฐ ๊ธธ์ด๋ฅผ ๊ธธ๊ฒ ์
๋ ฅํ๋ฉด ์ ํจ์๋ฆฌ ์ซ์๊น์ง๋ง ํํ
>>> nextBoolean() : ์
๋ ฅ ๋ฐ์ ๊ฐ์ boolean ํ์ผ๋ก ๋ฐํํจ
System.out.print("์ฐธ๊ณผ ๊ฑฐ์ง ์ค์ ํ ๊ฐ์ง๋ฅผ true or false๋ก ์
๋ ฅํ์ธ์ : ");
boolean isTrue = sc.nextBoolean();
System.out.println("์
๋ ฅํ์ ๋
ผ๋ฆฌ ๊ฐ์ " + isTrue + "์
๋๋ค.");
๐ Ref.
* true of false ์ธ์ ๋ค๋ฅธ ๊ฐ์ ์
๋ ฅํ๊ฒ ๋๋ฉด InputTypeMismatchException ๋ฐ์
sc.nextLine(); >>> ์ผ๋จ '๋ฌธ์์ด'๋ก ๋ฝ๊ธฐ
System.out.print("์๋ฌด ๋ฌธ์๋ ์
๋ ฅ ํด์ฃผ์ธ์ : ");
char ch = sc.nextLine().charAt(0);
>>> ์
๋ ฅํ ๋ฌธ์์ด์ ์ฒซ๋ฒ์งธ(0)์๋ฆฌ์ ๋ฌธ์๋ฅผ ๋ฝ์์ฃผ๋ ์
>>> (์ด์งํผ ๋ฌธ์ ํ๋๋ง ์
๋ ฅ์, ์ฒซ๋ฒ์งธ๋ ๊ทธ ๋ฌธ์ ํ๋์ด๋ฏ๋ก ๊ทธ ๋ฌธ์๊ฐ ๊ทธ๋๋ก ์ถ๋ ฅ๋จ)
System.out.println("์
๋ ฅํ์ ๋ฌธ์๋ " + ch + "์
๋๋ค.");
>>> Hyoyeon์ ์
๋ ฅํด๋, H๋ฅผ ์
๋ ฅํด๋ ์ฒซ๋ฒ์งธ ์๋ฆฌ์ ๋ฌธ์์ธ H๋ง ์ถ๋ ฅ๋จ
Scanner sc = new Scanner(System.in);
>>> Scanner ๊ฐ์ฒด ์์ฑ
>>> System.in : ํ์ค ์
๋ ฅ ์คํธ๋ฆผ
// nextLine()
>>> ๊ณต๋ฐฑ์ ํฌํจํ ํ ์ค์ ๊ทธ๋๋ก ์ถ๋ ฅ
System.out.print("์ธ์ฌ๋ง์ ์
๋ ฅํด์ฃผ์ญ์ผ : ");
String greeting1 = sc.nextLine();
System.out.println(greeting1);
// 2-2. next()
>>> ๊ณต๋ฐฑ ์ดํ์ ์ค์ ์ถ๋ ฅํ์ง์์
System.out.print("ํ๋ฒ ๋ ์ธ์ฌํด์ฃผ์ญ์ผ : ");
String greeting2 = sc.next();
System.out.println(greeting2);
"์๋
ํ์ธ์ ๋ฐ๊ฐ์ต๋๋ค" ์
๋ ฅ ์ ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๋ ๊ฐ์ ํ ํฐ "์๋
ํ์ธ์", "๋ฐ๊ฐ์ต๋๋ค"๋ก ๋ถ๋ฆฌ๊ฐ ๋จ
next()๋ ๋ค์ ํ ํฐ์ธ "์๋
ํ์ธ์"๋ฅผ ์ฝ์๊ณ nextInt()๋ ๋ค์ ํ ํฐ์ ์ ์๋ก ์ฝ์ด์ผ ํ๋๋ฐ ๋จ์์๋ "๋ฐ๊ฐ์ต๋๋ค"๊ฐ ์ฝํ์ InputMismatchException์ด ๋ฐ์ํ๋ค.
๋ฐ๋ผ์, ๊ณต๋ฐฑ์ด ์๋ ๋ฌธ์์ด์ ์
๋ ฅ๋ฐ์ ๊ฒฝ์ฐ์๋ nextLine()์ ์ฌ์ฉ
Scanner sc = new Scanner(System.in);
System.out.print("๋ฌธ์์ด์ ์
๋ ฅํด์ฃผ์ธ์ฉ : ");
>>> "์๋
ํ์ธ์ ๋ฐ๊ฐ์ต๋๋ค"๋ฅผ ์
๋ ฅ
String str1 = sc.next();
System.out.println("str1 : " + str1);
>>> "์๋
ํ์ธ์"๋ง ์ถ๋ ฅํ๋ฉด์ InputMismatchException ์ค๋ฅ ๋ฐ์
System.out.print("์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ฉ : ");
int num1 = sc.nextInt();
System.out.println("num1 : " + num1);
>>> ์ค๋ฅ๋ก ์ธํด ์ซ์ ์ถ๋ ฅ์ด ๋์ง์์
์์์ ๋จ๊ธด ๊ฐํ์ nextLine()์ด ์ฝ๊ณ ๋์ด๊ฐ์ ์๋ฌด๊ฒ๋ ์
๋ ฅํ์ง ์์๋๋ฐ ์ถ๋ ฅ๋๊ณ ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ
๋ฐ๋ผ์, ๋ฏธ๋ฆฌ ๋ ๋ค๋ฅธ nextLine()์ ํธ์ถํด์ ๋ฒํผ๋ฅผ ๋น์ฐ๊ณ ๋์
System.out.print("๋ค์ ์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์ฉ : ");
int num2 = sc.nextInt();
System.out.println("num2 : " + num2);
>>> ์ซ์๋ฅผ ์
๋ ฅํ ๋ค์ ์น 'enter'๊ฐ ์๋์ nextLine์ ์
๋ ฅ์ ๋ชป ํ๋๋ก ์ํฅ์ ์ค
sc.nextLine();
>>> ๋ฐ์์ ์
๋ ฅ์ ์ ์ ์๋์ํค๊ธฐ์ํด ๋ฏธ๋ฆฌ ํธ์ถํ nextLine
>>> ์ฌ๊ธฐ์ ์์์ ์
๋ ฅํ enter๋ฅผ ๋จน์ด๋ฒ๋ฆผ
System.out.println("๊ณต๋ฐฑ์ด ์๋ ๋ฌธ์์ด์ ํ๋ ์
๋ ฅํด์ฃผ์ธ์ฉ : ");
String str2 = sc.nextLine();
System.out.println("str2 : " + str2);
>>> ๋ง์ฝ ์์ ๋ฏธ๋ฆฌ ํธ์ถํ nextLine์ด ์๋ค๋ฉด, ์ฌ๊ธฐ์ ์ฝ์์ฐฝ์ ์
๋ ฅ์ ํ๋ ค๊ณ ํด๋ ๋งํ์ ๋ชป ํจ
>>> ์๋ํ๋ฉด, nextLine์ ์์์ ์
๋ ฅํ enter๋ฅผ ๋จน๋ ํน์ฑ์ด ์๊ธฐ ๋๋ฌธ์ ์คํํ์ง์๊ณ ๊ทธ๋๋ก ์ข
๋ฃ๋์ด๋ฒ๋ฆผ