Lang/โ˜• Java

โ˜• Java * print, printf, println, Scanner - ์ž…์ถœ๋ ฅ ํ•ด๋ณด๊ธฐ!

___l_i_ * 2021. 1. 22. 15:58

Scanner์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ผญ ์ƒ๋‹จ์— java.util.Scanner์„ import ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค!

import java.util.Scanner;

 

๊ทธ ์ดํ›„์—๋Š” ์ƒ์„ฑ๋œ scanner๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด๊ฒƒ์ €๊ฒƒ ๊ฐ’์„ ๋ฐ›์•„์˜ค๋ฉด ๋˜๋Š”๋ฐ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๋ฐ›๊ณ  ์ถœ๋ ฅํ•ด๋ณด์ž!

import java.util.Scanner;

class TestScanner {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // scanner ๊ฐ์ฒด ์ƒ์„ฑ

        System.out.print("Enter your name : ");
        String name = scanner.next();
        System.out.println("Your name is \"" + name + "\"");

        System.out.println();
        
        System.out.print("Enter your age : ");
        int i = scanner.nextInt();
        System.out.println("Your your age is \"" + i + "\"");
    }
}

๊ฐ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๋Š” ํ–‰์„ ์ž˜ ๋ณด๋ฉด (8ํ–‰, 14ํ–‰) scanner ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ช…์ด ๋‹ค๋ฅธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค!

์ž…๋ ฅ๋ฐ›๋Š” ๋ฐ์ดํ„ฐ์˜ ์ž๋ฃŒํ˜•์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ช…์ด ๋‹ค๋ฅด๋‹ˆ ์‚ฌ์šฉ ์‹œ ์ฐธ๊ณ ํ•˜๋„๋ก ํ•˜์ž!

 

 

 

๋” ์•Œ์•„๋ณด๊ธฐ

์ถœ๋ ฅ ๋ฉ”์†Œ๋“œ print ์™€ println ์˜ ์ฐจ์ด์ ์€?

print ๋Š” ์‹คํ–‰ ์‹œ ๊ฐœํ–‰์„ ์ง€ ์•Š๊ณ , println์€ ์ถœ๋ ฅ ์‹œ ๊ฐœํ–‰์„ ํ•œ๋‹ค!

class TestScanner {
    public static void main(String[] args) {
        System.out.print("This is");
        System.out.print("the print method");
        System.out.print("action");
        System.out.println("\n");
        /* 6ํ–‰์€ ๊ตฌ๋ถ„์„ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ผ๋ถ€๋Ÿฌ ๊ฐœํ–‰์„ ์‹œํ‚จ ๋ถ€๋ถ„์ž„์œผ๋กœ
        ์ด๋Œ€๋กœ ์‹คํ–‰์„ ์‹œ์ผœ๋ณด์‹œ๊ณ , 6ํ–‰์„ ์ฃผ์„ ์‹œ์ผœ์„œ๋„ ์‹คํ–‰ํ•ด ๋ณด์„ธ์š”! */
        System.out.println("This is");
        System.out.println("the println method");
        System.out.println("action");
    }
}
๋”๋ณด๊ธฐ
๊ทธ๋ฆผ2. 6ํ–‰ ์ฝ”๋“œ๋ฅผ ์ฃผ์„ํ•˜์ง€ ์•Š๊ณ  ์‹คํ–‰

 

์ถœ๋ ฅ ๋ฉ”์†Œ๋“œ printf๋„ ์•Œ์•„๋ณด์ž!

๊ฐœํ–‰์„ ํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์„œ์‹๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅ์„ ๋„์™€์ค€๋‹ค! print์™€ println์—์„œ๋Š” ์„œ์‹๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค!

์–ด๋–ค ์„œ์‹ ๋ฌธ์ž๊ฐ€ ์žˆ๋Š์ง€๋Š” ์ž˜ ์ •๋ฆฌํ•ด ๋†“์œผ์‹  ๋ถ„์ด ์žˆ๋‹ค. ๐Ÿ‘‰ [JAVA] System.out.printf์˜ ์„œ์‹๋ฌธ์ž(๋ณ€ํ™˜๋ฌธ์ž) ์ •๋ฆฌ

๋ฌผ๋ก  ๊ณต์‹ ๋ฌธ์„œ๋„ ์žˆ๋‹ค. ์˜๋ฌธ์ด๋‹ˆ ๋งˆ์Œ์˜ ์ค€๋น„ํ•˜๊ณ  ๋“ค์–ด๊ฐ€์ž. ๐Ÿ‘‰ ORACLE Java Documentation - Formatting Numeric Print Output

public class PrintfTest {
    public static void main(String[] args) {
        System.out.printf("%d, %s, %f", 1, "hi", 1.4);
    }
}

 

์–ด๋–ค ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋Š๋ƒ?

๊ทธ๊ฑฐ์•ผ ๊ณ„์† ์“ฐ๋‹ค๋ณด๋ฉด ์ด๊ฒŒ ํŽธํ•˜๋‹ค! ๋ผ๋Š” ๋Š๋‚Œ์ด ์˜จ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์ž๋ฅด๋Š” ๋„๊ตฌ์—๋„ ๊ฐ€์œ„, ์นผ, ํ•‘ํ‚น(?)๊ฐ€์œ„ ๋“ฑ๋“ฑ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋„๊ตฌ๊ฐ€ ์žˆ์ง€๋งŒ ์–ด๋–ค ๋ชจ์–‘์„ ๋‚ด๊ณ  ๋ฌด์—‡์„ ์ž๋ฅด๋ƒ์— ๋”ฐ๋ผ์„œ ํŽธํ•œ ๋„๊ตฌ๊ฐ€ ๋‹ค๋ฅด๋‹ค. ๋‹ค ์ ์žฌ์ ์†Œ์— ์“ฐ์ธ๋‹ค =ใ……=+

 

 

 


Thanks for ๐Ÿ™ˆ

  • Teacher. Park Min