๐ ์ฝ์ ์ ๋ ฅ
โ Scanner ํด๋์ค
- ์๋ฐ๋ ์
์ถ๋ ฅ ์คํธ๋ฆผ System.in, System.out, System.err ์ ๊ณต, ๋ฌธ์์ด์ ๋์ ์ผ๋ก ๋ฃ์ด์ค
(System.in์ ์ฝ์๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ ๋ฐ๋๋ฐ ์ฌ์ฉ) - ์ฌ์ฉ์ : type ๋ณ์ = ์ค์บ๋ํด๋์ค.๋ฉ์๋ ํ์์ผ๋ก ์ฌ์ฉ
int age = scanner.nextInt();
- ๊ธฐ๋ณธ์ ์ผ๋ก ์คํ์ด์ค๋ก ์
๋ ฅ ๊ฐ ๊ตฌ๋ถ, nextLine()์ ์ํฐ๋ก ์
๋ ฅ๊ฐ ๊ตฌ๋ถํ๊ณ ์คํ์ด์ค๋ ๋ฌธ์์ด๋ก ์ทจ๊ธํจ
๐ต ์๋ฃํ ๋ฉ์๋
์ ์ํ nextByte() ์ ๋ ฅ๊ฐ byteํ์ผ๋ก ๋ฐํ nextShort() ์ ๋ ฅ๊ฐ shortํ์ผ๋ก ๋ฐํ nextInt() ์ ๋ ฅ๊ฐ intํ์ผ๋ก ๋ฐํ nextLong() ์ ๋ ฅ๊ฐ longํ์ผ๋ก ๋ฐํ ์ค์ํ nextFloat() ์ ๋ ฅ๊ฐ floatํ์ผ๋ก ๋ฐํ nextDouble() ์ ๋ ฅ๊ฐ doubleํ์ผ๋ก ๋ฐํ ๋ฌธ์ํ next() ์ ๋ ฅ ๋ผ์ธ ๋ฌธ์์ด ๋ฐํ nextLine() ์ ๋ ฅ ๋ผ์ธ ์ ์ฒด ๋ฌธ์์ด ๋ฐํ(์คํ์ด์ค ํฌํจ)
๐ฐ ์ฝ์ ์ ๋ ฅ์ฐฝ ํ์ฑํ๋ฅผ ์ํ Vscode ์ค์ ๋ณ๊ฒฝ ๋ฐฉ๋ฒ
- ์ค์ : java console ๊ฒ์ : externalTerminal ์ผ๋ก ์์
๐์ฝ์์ ๋ ฅ ์๋ฃํ ๋ฉ์๋ ์์
package section18;
import java.util.Scanner;
public class Practice1803 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// ์ ์ ํ Scanner ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋์ ๊ฐ์ด ์
๋ ฅ/์ถ๋ ฅ์ด ๋๊ฒ ํ์์ค!
/*
* ์ด๋ฆ์ ์
๋ ฅํ์ธ์: ๊น์ผ๋จ
* ๋์ด๋ฅผ ์
๋ ฅํ์ธ์: 99
* ํค๋ฅผ ์
๋ ฅํ์ธ์: 169.9
* ์ด๋ฆ: ๊น์ผ๋จ, ๋์ด: 99, ํค: 169.90
*/
System.out.print("์ด๋ฆ์ ์
๋ ฅํ์ธ์: ");
String name = scanner.nextLine();
System.out.print("๋์ด๋ฅผ ์
๋ ฅํ์ธ์: ");
int age = scanner.nextInt();
System.out.print("ํค๋ฅผ ์
๋ ฅํ์ธ์: ");
double height = scanner.nextDouble();
System.out.printf("์ด๋ฆ: %s, ๋์ด: %d, ํค: %.1f\n", name, age, height);
scanner.close();
}
}
๐ ์ถ๋ ฅ ๋ช ๋ น ( System.out.printf(); )
System.out.print(); | ์ธ์๋ฅผ ๋ฌธ์์ด๋ก ์ฝ์ ์ถ๋ ฅ |
System.out.println(); | ์ธ์๋ฅผ ๋ฌธ์์ด๋ก ์ฝ์ ์ถ๋ ฅ + ๊ฐํ(์ค๋ฐ๊ฟ) |
System.out.printf(); | ์์ ๋ฌธ์ ์ฌ์ฉ ์ถ๋ ฅ(๋ฌธ์์ด ํฌ๋งคํ ) |
๐ต ์์ ๋ฌธ์๋ณ ์ถ๋ ฅ ํ
%d | ์ ์(10์ง์) |
%o | ์ ์(8์ง์) |
%x | ์ ์(16์ง์) |
%f | ์ค์ |
%e | ์ง์ |
%g | %e ๋๋ %f ํํ๋ก ์ถ๋ ฅ |
%s | ๋ฌธ์์ด |
%c | ๋ฌธ์ |
%% | Literal %(๋ฌธ์์ด๋ก ์ทจ๊ธ) |
๐ ์์ ๋ฌธ์๋ณ ๋จ์ ์ถ๋ ฅ๋ฌธ ์์
- Ex.๋ง๋ ์์๋ฌธ์ ๋ฃ๊ณ ์ถ๋ ฅํ๊ธฐ
package section18;
public class Practice1801 {
public static void main(String[] args) {
int age = 25;
double height = 175.5;
String city = "Busan";
//๋์ด,ํค,๋์ ๊ฐ ๋์ค๋๋ก ๋์ด:? , ํค: ? .. ์์ฑํ๊ธฐ
System.out.printf("๋์ด:%d, ํค: %.1f, ๋์: %s \n", age,height,city);
}
}
-Ex) ์ฌ๋ฌ ๊ฐ์ ์์ ๋ฌธ์ ์ฌ์ฉ
package section18;
public class Ex1803 {
public static void main(String[] args) {
System.out.println(String.format("I eat %d apples.", 5 ));
//format ์์ ๋ฌธ์(%d)๋ฅผ ์ฌ์ฉํ๊ฒ ํด์ฃผ๋ ๋ฉ์๋
//์์ string format ์ฐ๋๊ฒ ๋ฒ๊ฑฐ๋ก์์ ๋์จ๊ฒ ์๋์ printf(์๋ ์ค๋ฐ๊ฟ์ด ์๋๋ฏ๋ก ํ์์ \n์ ๋ถํ)
System.out.printf(String.format("๋๋ %d ์์ ์ ๋
์ ๋จน์๊ฒ์ด๋ค \n",8));
System.out.printf(String.format("๋ด ์๋ ฅ์ %.1f ์ด๋ค \n",1.2));
System.out.printf("i eat %d apples. \n", 5);
//์ค์ ๋ฃ๊ธฐ : ์ค์์ฉ ์์๋ฌธ์๋ก ๋ฐ๊ฟ์ผํจ (ํํํ๊ณ ์ํ๋ ์์ซ์ ์๋ฆฌ๊น์ง๋ฅผ %.n ์ผ๋ก ํ๊ธฐ)
System.out.printf("i eat %f apples. \n", 2.5);
System.out.printf("i eat %.2f apples. \n", 2.5);
System.out.printf("i eat %s apples. \n", "five");
System.out.printf("i eat %s apples. \n", "2.50");
//%๋ฅผ ๋ถํ๋ ค๋ฉด %%๋ก ํจ
System.out.printf("i have completed %d%% of my Java studies.\n", 50);
//์ซ์ ๊ธ์ ๊ฐ์ด ์์๋ฌธ์๋ก ๋ํ๋ด๊ธฐ
System.out.printf("i have completed %d%% of my %s studies.\n", 100,"Java");
System.out.printf("i have completed %3d%% of my %s studies.\n", 90,"Java");
System.out.printf("i have completed %-3d%% of my %s studies.\n", 20,"Java");
//%3d%%, %-3d%% ์ฒ๋ผ 3์นธ์ ๊ณต๊ฐ์ ํ๋ณดํ๋ฉฐ ์์๋ฌธ์์ ๊ณต๊ฐํ๋ณด๋์ ์ง์ ๊ฐ๋ฅ
System.out.printf("i have completed %.2f%% of my %s studies.\n", 99.9999,"Java");
//๋ฐ์ฌ๋ฆผ์ ํจ = 99.9999๋ 100.00%๋ก ํํ๋จ (๋ฐ์ฌ๋ฆผ์ด ์ซ์ผ๋ฉด ๋ฌธ์์ด๋ก ๋ฃ์ด์ผํจ -> "99.9999","Java")
}
}
- Ex. ์์ ๋ฌธ์์ ๋ณ์์ ์ฌ์ฉ (Scanner ์ฌ์ฉํด์ ์ง์ ์ ๋ ฅํด๋ณด๊ธฐ)
package section18;
import java.util.Scanner;
public class Practice1803 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// ์ ์ ํ Scanner ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋์ ๊ฐ์ด ์
๋ ฅ/์ถ๋ ฅ์ด ๋๊ฒ ํ์์ค!
/*
* ์ด๋ฆ์ ์
๋ ฅํ์ธ์: ๊น์ผ๋จ
* ๋์ด๋ฅผ ์
๋ ฅํ์ธ์: 99
* ํค๋ฅผ ์
๋ ฅํ์ธ์: 169.9
* ์ด๋ฆ: ๊น์ผ๋จ, ๋์ด: 99, ํค: 169.90
*/
System.out.print("์ด๋ฆ์ ์
๋ ฅํ์ธ์: ");
String name = scanner.nextLine();
System.out.print("๋์ด๋ฅผ ์
๋ ฅํ์ธ์: ");
int age = scanner.nextInt();
System.out.print("ํค๋ฅผ ์
๋ ฅํ์ธ์: ");
double height = scanner.nextDouble();
System.out.printf("์ด๋ฆ: %s, ๋์ด: %d, ํค: %.1f\n", name, age, height);
scanner.close();
}
}
๐ฐ String format ๊น์ง ์จ์ฃผ๋๊ฒ ๋ฒ๊ฑฐ๋ก์ฐ๋ฏ๋ก, println (์๋ ๊ฐํ) ๋์ printf ๋ฅผ ์ฌ์ฉํ๋ฉฐ
๋ด์ฉ์ \n ์ ๋ถํ ์๋ ์ค๋ฐ๊ฟ์ ํ๋์์ผ๋ก ์ฌ์ฉ
System.out.printf("๋์ ํค๋ %d Cm์ด๋ค.\n", 165);
๐ฐ์นธ์ ํ๋ณด ํ๋ ค๋ฉด ์์๋ฌธ์์ -n์ด๋ n์ ๋ถํ
System.out.printf("i have completed %d%% of my %s studies.\n", 100,"Java");
System.out.printf("i have completed %3d%% of my %s studies.\n", 90,"Java");
๐ฐ์ซ์์ ๋ฐ์ฌ๋ฆผ์ด ์๋์ผ๋ก ๋ฐ์๋๋ฏ๋ก, ๋ฌธ์์ด์ผ๋ก ์์ ํ๋ค
(Ex. 99.999 ์ซ์์ ๋ฐ์ฌ๋ฆผ ์ด์)
System.out.printf("i have completed %.2f%% of my %s studies.\n", 99.9999,"Java");
์ด๋ ๊ฒ ์ถ๋ ฅํ๋ ๊ฒฝ์ฐ ์์๋ฌธ์๋ฅผ %.2f (์ฆ, ์์ซ์ 2์๋ฆฌ๊น์ง ํํ) %%๋ก ํ๊ธฐ ๋๋ฌธ์
๋ฐ์ฌ๋ฆผ ํ๋ ํน์ฑ์, 100.00% ๋๋๋ฐ, ์ด๋ฅผ ๋ฐฉ์งํ๋ ค๋ฉด 99.999๋ฅผ ๋ฌธ์์ด์ธ "99.999"๋ก ๋ฃ์ด์ผ ํจ