๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Today I Learned(TIL)/Java

Java ์ž…๋ฌธ ๊ฐ•์˜_ํ›ˆ๋ จ

by carrot0911 2024. 11. 6.

Scanner ํ•™์Šต

ํ›ˆ๋ จ ์‹œ์ž‘

์ง€๊ธˆ๊นŒ์ง€ ํ•™์Šตํ•œ ๋ณ€์ˆ˜, ์—ฐ์‚ฐ์ž, ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋žจ ์–ธ์–ด๋Š” ์ด ๊ธฐ๋Šฅ์„ ํ•„์ˆ˜๋กœ ๊ฐ€์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ํ•˜๋Š” ์ผ์˜ ๋Œ€๋ถ€๋ถ„์€ ์ง€๊ธˆ๊นŒ์ง€ ์„ค๋ช…ํ•œ ๋ณ€์ˆ˜, ์—ฐ์‚ฐ์ž, ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ์„ ๋‹ค๋ฃจ๋Š” ์ผ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๊ธฐ๋Šฅ์„ ์ž˜ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์ด ๋ฌด์—‡๋ณด๋‹ค ์ค‘์š”ํ•˜๋‹ค.

โญ๋ฐฑ๋ฌธ์ด ๋ถˆ์—ฌ์ผํƒ€!โญ 

๋ณ€์ˆ˜, ์—ฐ์‚ฐ์ž, ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ์„ ๋จธ๋ฆฌ๋กœ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์€ ์ „ํ˜€ ์–ด๋ ต์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ ๋จธ๋ฆฌ๋กœ ์ƒ๊ฐ๋งŒ ํ•˜๋Š” ๊ฒƒ์€ ์ˆ˜์˜์„ ์ด๋ ‡๊ฒŒ ํ•ด์•ผ ํ•˜๊ฒ ์ง€?๋ผ๊ณ  ๋จธ๋ฆฌ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ฝ”๋”ฉ์„ ๋ชธ์ด ์ตํžˆ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ง์ ‘ ์ฝ”๋”ฉํ•˜๋Š” ๊ฒƒ์ด ๋ฌด์—‡๋ณด๋‹ค ์ค‘์š”ํ•˜๋‹ค! ํ•™์ƒ ๋•Œ์ฒ˜๋Ÿผ ๋‹จ์ˆœํžˆ ์™ธ์šฐ๋Š” ๋ฐฉ์‹์œผ๋กœ๋Š” ์ข‹์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค. ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ ๋”ฐ๋ผ ํ•ด ๋ณด๊ณ , ๋ฌธ์ œ๋„ ์ง์ ‘ ๋‹ค ํ’€์–ด๋ณด์ž. ๋ฌธ์ œ๊ฐ€ ์•ˆ ํ’€๋ฆฌ๋ฉด ๋‹ต์„ ๋ณด๊ณ  ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ์นœ ๋‹ค์Œ์— ๊ธฐ์กด ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ ์ง€์šฐ๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ณธ์ธ์ด ์Šค์Šค๋กœ ๋‹ค์‹œ ํ’€์–ด๋ณด์•„๋„ ์ข‹๋‹ค. ๋ฐฑ๋ฌธ์ด ๋ถˆ์—ฌ์ผํƒ€!

Scanner

System.out์„ ํ†ตํ•ด์„œ ์ถœ๋ ฅ์„ ํ–ˆ๋“ฏ์ด, System.in์„ ํ†ตํ•ด์„œ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ž๋ฐ”๊ฐ€ ์ œ๊ณตํ•˜๋Š” System.in์„ ํ†ตํ•ด์„œ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ ค๋ฉด ์—ฌ๋Ÿฌ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•ด์„œ ๋ณต์žกํ•˜๊ณ  ์–ด๋ ต๋‹ค.
์ž๋ฐ”๋Š” ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด Scanner๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋งค์šฐ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

Scanner ์˜ˆ์ œ 1

Scanner1

package scanner;

import java.util.Scanner;

public class Scanner1 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        String str = scanner.nextLine();  //์ž…๋ ฅ์„ String์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
        System.out.println("์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด : " + str);

        System.out.print("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int intValue = scanner.nextInt();  //์ž…๋ ฅ์„ int๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
        System.out.println("์ž…๋ ฅํ•œ ์ •์ˆ˜ : " + intValue);

        System.out.print("์‹ค์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        double doubleValue = scanner.nextDouble();  //์ž…๋ ฅ์„ double๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
        System.out.println("์ž…๋ ฅํ•œ ์‹ค์ˆ˜ : " + doubleValue);
    }
}
  • Scanner scanner = new Scanner(System.in);
    • ์ด ์ฝ”๋“œ๋Š” ๊ฐ์ฒด์™€ ํด๋ž˜์Šค๋ฅผ ๋ฐฐ์›Œ์•ผ ์ •ํ™•ํžˆ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ง€๊ธˆ์€ Scanner์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ new๋ฅผ ์‚ฌ์šฉํ•ด์„œ Scanner๋ฅผ ๋งŒ๋“ ๋‹ค ์ •๋„๋กœ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. Scanner๋Š” System.in์„ ์‚ฌ์šฉํ•ด์„œ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋ฐ›๋„๋ก ๋„์™€์ค€๋‹ค.
    • Scanner scanner ์ฝ”๋“œ๋Š” scanner ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด์ œ๋ถ€ํ„ฐ scanner ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด์„œ scanner๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • scanner.nextLine()
    • ์—”ํ„ฐ(\n)๋ฅผ ์ž…๋ ฅํ•  ๋•Œ๊นŒ์ง€ ๋ฌธ์ž๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
  • scanner.nextInt()
    • ์ž…๋ ฅ์„ intํ˜•์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค. ์ •์ˆ˜ ์ž…๋ ฅ์— ์‚ฌ์šฉํ•œ๋‹ค.
  • scanner.nextDouble()
    • ์ž…๋ ฅ์„ doubleํ˜•์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค. ์‹ค์ˆ˜ ์ž…๋ ฅ์— ์‚ฌ์šฉํ•œ๋‹ค.

์ฃผ์˜! - ๋‹ค๋ฅธ ํƒ€์ž… ์ž…๋ ฅ ์‹œ ์˜ค๋ฅ˜

ํƒ€์ž…์ด ๋‹ค๋ฅด๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์˜ˆ์ œ์™€ ๊ฐ™์ด ์ˆซ์ž์— ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : hello
์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด : hello
์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ๋ฐฑ๋งŒ์›
Exception in thread "main" java.util.InputMismatchException
	at java.base/java.util.Scanner.throwFor(Scanner.java:964)
	at java.base/java.util.Scanner.next(Scanner.java:1619)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2284)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2238)
	at scanner.Scanner1.main(Scanner1.java:15)

print() vs println()

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด println( )์ด ์•„๋‹ˆ๋ผ print( )๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
System.out.print("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")
๊ทธ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

print( ) ์ถœ๋ ฅํ•˜๊ณ  ๋‹ค์Œ ๋ผ์ธ์œผ๋กœ ๋„˜๊ธฐ์ง€ ์•Š๋Š”๋‹ค.

System.out.print("hello");
System.out.print("world");
// ๊ฒฐ๊ณผ : helloworld

println( ) ์ถœ๋ ฅํ•˜๊ณ  ๋‹ค์Œ ๋ผ์ธ์œผ๋กœ ๋„˜๊ธด๋‹ค.

System.out.println("hello");
System.out.println("world");
// ๊ฒฐ๊ณผ : 
hello
world

์šฐ๋ฆฌ๊ฐ€ ์—”ํ„ฐ ํ‚ค๋ฅผ ์น˜๋ฉด (\n)์ด๋ผ๋Š” ๋ฌธ์ž๋ฅผ ๋‚จ๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค.
์ด ๋ฌธ์ž๋Š” ์˜์–ด๋กœ new line character, ํ•œ๊ธ€๋กœ ์ค„ ๋ฐ”๊ฟˆ ๋˜๋Š” ๊ฐœํ–‰ ๋ฌธ์ž๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์ƒˆ๋กœ์šด ๋ผ์ธ์œผ๋กœ ๋„˜๊ธฐ๋ผ๋Š” ๋œป์ด๋‹ค. ์ฝ˜์†”์—์„œ๋Š” ์ด ๋ฌธ์ž๋ฅผ ๋ณด๊ณ  ๋‹ค์Œ ๋ผ์ธ์œผ๋กœ ๋„˜๊ธด๋‹ค.
println( )์€ print( )์˜ ๋งˆ์ง€๋ง‰์— \n์„ ์ถ”๊ฐ€ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ ์ฝ”๋“œ๋Š” println( )๊ณผ ๊ฐ™๋‹ค.

System.out.print("hello\n");
System.out.print("world\n");
// ๊ฒฐ๊ณผ :
hello
world

 

Scanner - ๊ธฐ๋ณธ ์˜ˆ์ œ

Scanner ์˜ˆ์ œ 2

Scanner2

package scanner;

import java.util.Scanner;

public class Scanner2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int num1 = scanner.nextInt();

        System.out.print("๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int num2 = scanner.nextInt();

        int sum = num1 + num2;
        System.out.println("๋‘ ์ˆซ์ž์˜ ํ•ฉ : " + sum);
    }
}

Scanner ์˜ˆ์ œ 3

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ๋” ํฐ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์ž. ๋‘ ์ˆซ์ž๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ ๋‘ ์ˆซ์ž๋Š” ๊ฐ™๋‹ค๊ณ  ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.
์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Scanner3

package scanner;

import java.util.Scanner;

public class Scanner3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("์ฒซ ๋ฒˆ์งธ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int num1 = scanner.nextInt();

        System.out.print("๋‘ ๋ฒˆ์งธ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int num2 = scanner.nextInt();

        if (num1 > num2) {
            System.out.println("๋” ํฐ ์ˆซ์ž : " + num1);
        } else if (num1 < num2) {
            System.out.println("๋” ํฐ ์ˆซ์ž : " + num2);
        } else {
            System.out.println("๋‘ ์ˆซ์ž๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.");
        }
    }
}


Scanner - ๋ฐ˜๋ณต ์˜ˆ์ œ

์ง€๊ธˆ๊นŒ์ง€ ๊ฐœ๋ฐœํ•œ ํ”„๋กœ๊ทธ๋žจ๋“ค์€ ๋‹จ ํ•œ ๋ฒˆ์˜ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ํ›„ ์ข…๋ฃŒ๋˜๋Š” ์ผํšŒ์„ฑ ํ”„๋กœ๊ทธ๋žจ์ด์—ˆ๋‹ค. ์‹ค์ œ ํ”„๋กœ๊ทธ๋žจ๋“ค์€ ์ด๋ ‡๊ฒŒ ํ•œ ๋ฒˆ์˜ ๊ฒฐ๊ณผ๋งŒ ์ถœ๋ ฅํ•˜๊ณ  ์ข…๋ฃŒ๋˜์ง€ ์•Š๋Š”๋‹ค. ํ•œ ๋ฒˆ ์‹คํ–‰ํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰๋œ๋‹ค. ์ด์ œ๋ถ€ํ„ฐ๋Š” ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์ง€์†ํ•ด์„œ ๋ฐ›์•„๋“ค์ด๊ณ , ๋ฐ˜๋ณตํ•ด์„œ ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•ด ๋ณด์ž.

Scanner์™€ ๋ฐ˜๋ณต๋ฌธ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

Scanner ๋ฐ˜๋ณต ์˜ˆ์ œ 1

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.
  • exit๋ผ๋Š” ๋ฌธ์ž๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•œ๋‹ค.
  • ํ”„๋กœ๊ทธ๋žจ์€ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰๋œ๋‹ค.

ScannerWhile1

package scanner;

import java.util.Scanner;

public class ScannerWhile1 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print("๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜์„ธ์š”(exit : ์ข…๋ฃŒ) : ");
            String str = scanner.nextLine();

            if (str.equals("exit")) {
                System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
                break;
            }
            System.out.println("์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด : " + str);
        }
    }
}

while (true) : ์ค‘๊ฐ„์— break ๋ฌธ์„ ๋งŒ๋‚˜๊ธฐ ์ „๊นŒ์ง€ ๋ฌดํ•œ ๋ฐ˜๋ณตํ•œ๋‹ค.

if (str.equals("exit")) {
    System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
    break;
}

์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž๊ฐ€ "exit"์ด๋ฉด "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค."๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  break ๋ฌธ์„ ํ†ตํ•ด์„œ while๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค.

Scanner ๋ฐ˜๋ณต ์˜ˆ์ œ 2

  • ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž์™€ ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๋”ํ•ด์„œ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•˜์ž.
  • ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž์™€ ๋‘ ๋ฒˆ์งธ ์ˆซ์ž ๋ชจ๋‘ 0์„ ์ž…๋ ฅํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•œ๋‹ค.
  • ํ”„๋กœ๊ทธ๋žจ์€ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰๋œ๋‹ค.

ScannerWhile2

package scanner;

import java.util.Scanner;

public class ScannerWhile2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž์™€ ๋‘ ๋ฒˆ์งธ ์ˆซ์ž ๋ชจ๋‘ 0์„ ์ž…๋ ฅํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");

        while (true) {
            System.out.print("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
            int num1 = scanner.nextInt();

            System.out.print("๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
            int num2 = scanner.nextInt();

            if (num1 == 0 && num2 == 0) {
                System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
                break;
            }

            int sum = num1 + num2;
            System.out.println("๋‘ ์ˆซ์ž์˜ ํ•ฉ : " + sum);
        }
    }
}

์ข…๋ฃŒ ๋ถ€๋ถ„

if (num1 == 0 && num2 == 0) {
    System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
    break;
}

num1 == 0 && num2 == 0

  • ์ž…๋ ฅ๋ฐ›์€ num1, num2 ๋‘˜ ๋‹ค ํ•จ๊ป˜ 0์ผ ๋•Œ "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค."๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  break๋ฅผ ํ†ตํ•ด while๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค.
  • ๋น„๊ต ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
    true && true → true์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋‘ ์กฐ๊ฑด์ด ๋ชจ๋‘ ์ฐธ์ด์–ด์•ผ if ๋ฌธ์˜ ์ฝ”๋“œ ๋ธ”๋ก์ด ์‹คํ–‰๋œ๋‹ค.

Scanner ๋ฐ˜๋ณต ์˜ˆ์ œ 3

์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๋ฐ›์•„ ๊ทธ ํ•ฉ๊ณ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.
์‚ฌ์šฉ์ž๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ 0์„ ์ž…๋ ฅํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์€ ์ข…๋ฃŒ๋œ๋‹ค. ์ด๋•Œ, ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ชจ๋“  ์ •์ˆ˜์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

ScannerWhile3

package scanner;

import java.util.Scanner;

public class ScannerWhile3 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int sum = 0;

        while (true) {
            System.out.print("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(0์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : ");
            int num = input.nextInt();

            if (num == 0) {
                System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
                break;
            }

            sum += num;
        }
        System.out.println("์ž…๋ ฅํ•œ ๋ชจ๋“  ์ •์ˆ˜์˜ ํ•ฉ : " + sum);
    }
}
  • int sum : ์ด๊ณณ์— ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ๋ˆ„์ ํ•œ๋‹ค.
  • sum += num : ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ num ๊ฐ’์„ sum์— ๋ˆ„์ ํ•ด์„œ ๋”ํ•œ๋‹ค.
    • +=์„ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ ๋‹ค์Œ ์ฝ”๋“œ์™€ ๊ฐ™์•„. sum = sum + num

 

๋ฌธ์ œ์™€ ํ’€์ด 1

๋ฌธ์ œ - ์ด๋ฆ„ ๋‚˜์ด ์ž…๋ ฅ๋ฐ›๊ณ  ์ถœ๋ ฅํ•˜๊ธฐ

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”. ์ถœ๋ ฅ ํ˜•ํƒœ๋Š” "๋‹น์‹ ์˜ ์ด๋ฆ„์€ [์ด๋ฆ„]์ด๊ณ , ๋‚˜์ด๋Š” [๋‚˜์ด] ์‚ด์ž…๋‹ˆ๋‹ค."์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

๋‹น์‹ ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”. ์ž๋ฐ”
๋‹น์‹ ์˜ ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 30
๋‹น์‹ ์˜ ์ด๋ฆ„์€ ์ž๋ฐ”์ด๊ณ , ๋‚˜์ด๋Š” 30์‚ด์ž…๋‹ˆ๋‹ค.

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerEx1 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("๋‹น์‹ ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        String name = scanner.nextLine();

        System.out.print("๋‹น์‹ ์˜ ๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int age = scanner.nextInt();

        System.out.println("๋‹น์‹ ์˜ ์ด๋ฆ„์€ " + name + "์ด๊ณ , ๋‚˜์ด๋Š” " + age + "์‚ด์ž…๋‹ˆ๋‹ค.");
    }
}

๋ฌธ์ œ - ํ™€์ˆ˜ ์ง์ˆ˜

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ์ด ์ •์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ์ง€ ์ง์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

ํ•˜๋‚˜์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 1
์ž…๋ ฅํ•œ ์ˆซ์ž 1๋Š” ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

ํ•˜๋‚˜์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 4
์ž…๋ ฅํ•œ ์ˆซ์ž 4๋Š” ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerEx2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("ํ•˜๋‚˜์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int num = scanner.nextInt();

        if (num % 2 == 0) {
            System.out.println("์ž…๋ ฅํ•œ ์ˆซ์ž " + num + "์€(๋Š”) ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.");
        } else {
            System.out.println("์ž…๋ ฅํ•œ ์ˆซ์ž " + num + "์€(๋Š”) ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.");
        }
    }
}

๋ฌธ์ œ - ์Œ์‹์  ์ฃผ๋ฌธ

  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์Œ์‹์˜ ์ด๋ฆ„ (foodName), ๊ฐ€๊ฒฉ(foodPrice), ๊ทธ๋ฆฌ๊ณ  ์ˆ˜๋Ÿ‰(foodQuantity)์„ ์ž…๋ ฅ๋ฐ›์•„, ์ฃผ๋ฌธํ•œ ์Œ์‹์˜ ์ด๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
  • ์Œ์‹์˜ ์ด๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•˜์„ธ์š”. ์ด๊ฐ€๊ฒฉ์€ ๊ฐ ์Œ์‹์˜ ๊ฐ€๊ฒฉ(foodPrice)๊ณผ ์ˆ˜๋Ÿ‰(foodQuantity)์„ ๊ณฑํ•œ ๊ฐ’์ด๋ฉฐ, ์ด๋ฅผ totalPrice๋ผ๋Š” ์ด๋ฆ„์˜ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์„ธ์š”.
  • ์ฃผ๋ฌธ ์ •๋ณด์™€ ์ด๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•˜์„ธ์š”. ์ถœ๋ ฅ ํ˜•ํƒœ๋Š” "[์Œ์‹ ์ด๋ฆ„] [์ˆ˜๋Ÿ‰] ๊ฐœ๋ฅผ ์ฃผ๋ฌธํ•˜์…จ์Šต๋‹ˆ๋‹ค. ์ด๊ฐ€๊ฒฉ์€ [์ด ๊ฐ€๊ฒฉ] ์›์ž…๋‹ˆ๋‹ค."์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

์Œ์‹ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ํ”ผ์ž
์Œ์‹์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : 20000
์Œ์‹์˜ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : 2
ํ”ผ์ž 2๊ฐœ๋ฅผ ์ฃผ๋ฌธํ•˜์…จ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€๊ฒฉ์€ 40000์›์ž…๋‹ˆ๋‹ค.

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerEx3 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("์Œ์‹ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ");
        String foodName = input.nextLine();

        System.out.print("์Œ์‹์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ");
        int foodPrice = input.nextInt();

        System.out.print("์Œ์‹์˜ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ");
        int foodQuantity = input.nextInt();

        int totalPrice = foodPrice * foodQuantity;

        System.out.println(foodName + " " + foodQuantity + "๊ฐœ๋ฅผ ์ฃผ๋ฌธํ•˜์…จ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€๊ฒฉ์€ " + totalPrice + "์›์ž…๋‹ˆ๋‹ค.");
    }
}

 

โœ”๏ธ ์ฐธ๊ณ 

๋ณ€์ˆ˜ ์ „์ฒด ๋ฐ”๊พธ๊ธฐ : Shift + F6

 

๋ฌธ์ œ - ๊ตฌ๊ตฌ๋‹จ ์ถœ๋ ฅ

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ์ •์ˆ˜ n์„ ์ž…๋ ฅ๋ฐ›๊ณ , ์ž…๋ ฅ๋ฐ›์€ ์ •์ˆ˜ n์˜ ๊ตฌ๊ตฌ๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

๊ตฌ๊ตฌ๋‹จ์˜ ๋‹จ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : 8
8๋‹จ์˜ ๊ตฌ๊ตฌ๋‹จ : 
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerEx4 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("๊ตฌ๊ตฌ๋‹จ์˜ ๋‹จ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” : ");
        int num = scanner.nextInt();

        System.out.println(num + "๋‹จ์˜ ๊ตฌ๊ตฌ๋‹จ : ");

        for (int i = 1; i <= 9; i ++) {
            int sum = num * i;
            System.out.println(num + " x " + i + " = " + sum);
        }
    }
}

 

๋ฌธ์ œ์™€ ํ’€์ด 2

๋ฌธ์ œ - ๋ณ€์ˆ˜ ๊ฐ’ ๊ตํ™˜

๋ณ€์ˆ˜ a = 10์ด ๋“ค์–ด์žˆ๊ณ , b = 20์ด ๋“ค์–ด์žˆ๋‹ค.
๋ณ€์ˆ˜ a์˜ ๊ฐ’๊ณผ ๋ณ€์ˆ˜ b์˜ ๊ฐ’์„ ์„œ๋กœ ๋ฐ”๊พธ์–ด๋ผ.
๋‹ค์Œ ์ฝ”๋“œ์—์„œ ์‹œ์ž‘๊ณผ ์ข…๋ฃŒ ๋ถ€๋ถ„ ์‚ฌ์ด์— ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๊ตํ™˜ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.
ํžŒํŠธ : temp ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค.

์ถœ๋ ฅ ๊ฒฐ๊ณผ

a = 20
b = 10

์ •๋‹ต

package scanner.ex;

public class ChargeVarEx {

    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int temp;

        temp = a;
        a = b;
        b = temp;
        
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}

ํ’€์ด

  • a์™€ b๋ฅผ ํ•œ ๋ฒˆ์— ์„œ๋กœ ๊ตํ™˜ํ•  ์ˆ˜๋Š” ์—†๋‹ค.
  • a = b๋ผ๊ณ  ํ•˜๋ฉด a์˜ ๊ฐ’์ธ 10์€ ์‚ฌ๋ผ์ ธ ๋ฒ„๋ฆฐ๋‹ค. ๋”ฐ๋ผ์„œ a, b ๋‘˜ ๋‹ค 20์ด ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค.
  • a = b๋ผ๊ณ  ํ•˜๊ธฐ ์ „์— a์˜ ๊ฐ’์„ ์–ด๋”˜๊ฐ€์— ๋ณด๊ด€ํ•ด๋‘์–ด์•ผ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ž„์‹œ๋กœ ์‚ฌ์šฉํ•  ๋ณ€์ˆ˜์ธ temp์— ๋ณด๊ด€ํ•ด ๋‘์—ˆ๋‹ค.

๋ฌธ์ œ - ์‚ฌ์ด ์ˆซ์ž

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ์ด ๋‘ ์ •์ˆ˜ ์‚ฌ์ด์˜ ๋ชจ๋“  ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

  • ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ์„ธ์š”. ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ num1์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ์ž์—๊ฒŒ ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ์„ธ์š”. ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ num2์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž num1์ด ๋‘ ๋ฒˆ์งธ ์ˆซ์ž num2๋ณด๋‹ค ํฌ๋‹ค๋ฉด, ๋‘ ์ˆซ์ž๋ฅผ ๊ตํ™˜ํ•˜์„ธ์š”.
    • ์ฐธ๊ณ  : ๊ตํ™˜์„ ์œ„ํ•ด ์ž„์‹œ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์„ ๊ณ ๋ คํ•˜์„ธ์š”.
  • num 1๋ถ€ํ„ฐ num2๊นŒ์ง€์˜ ๊ฐ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
  • ์ถœ๋ ฅ ๊ฒฐ๊ณผ์— ์œ ์˜ํ•˜์„ธ์š”. ๋‹ค์Œ ์˜ˆ์‹œ์™€ ๊ฐ™์ด 2, 3, 4, 5์ฒ˜๋Ÿผ , ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 2
๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 5
๋‘ ์ˆซ์ž ์‚ฌ์ด์˜ ๋ชจ๋“  ์ •์ˆ˜ : 2, 3, 4, 5

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerEx5 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int num1 = scanner.nextInt();

        System.out.print("๋‘ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
        int num2 = scanner.nextInt();

        if (num1 > num2) {
            int temp;
            temp = num1;
            num1 = num2;
            num2 = temp;
        }

        System.out.print("๋‘ ์ˆซ์ž ์‚ฌ์ด์˜ ๋ชจ๋“  ์ •์ˆ˜ : ");

        for (int i = num1; i <= num2; i++) {
            System.out.print(i);

            if (i != num2) {
                System.out.print(", ");
            }
        }
    }
}

 

๋ฌธ์ œ์™€ ํ’€์ด 3

๋ฌธ์ œ - ์ด๋ฆ„๊ณผ ๋‚˜์ด ๋ฐ˜๋ณต

  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ž…๋ ฅ๋ฐ›๊ณ , ์ž…๋ ฅ๋ฐ›์€ ์ด๋ฆ„๊ณผ ๋‚˜์ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์‚ฌ์šฉ์ž๊ฐ€ "์ข…๋ฃŒ"๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹ค์Œ ์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š” (์ข…๋ฃŒ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : ์ž๋ฐ”
๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 30
์ž…๋ ฅํ•œ ์ด๋ฆ„ : ์ž๋ฐ”, ๋‚˜์ด : 30
์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š” (์ข…๋ฃŒ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : ํ•˜๋‹ˆ
๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : 20
์ž…๋ ฅํ•œ ์ด๋ฆ„ : ํ•˜๋‹ˆ, ๋‚˜์ด : 20
์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š” (์ข…๋ฃŒ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : ์ข…๋ฃŒ
ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerWhileEx1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while (true) {
            System.out.print("์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š” (์ข…๋ฃŒ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : ");
            String name = input.nextLine();

            if (name.equals("์ข…๋ฃŒ")) {
                System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
                break;
            }

            System.out.print("๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
            int age = input.nextInt();  // ์ˆซ์ž๋งŒ ๊ฐ€์ ธ๊ฐ€๊ณ  ์—”ํ„ฐ(\n)๊ฐ€ ๋‚จ์•„์žˆ์–ด์„œ ๋‹ค์Œ nextLine()์—์„œ ์—†์• ์ค˜์•ผ ํ•œ๋‹ค.
            input.nextLine();

            System.out.println("์ž…๋ ฅํ•œ ์ด๋ฆ„ : " + name + ", ๋‚˜์ด : " + age);
        }
    }
}

๋ฌธ์ œ - ์ƒํ’ˆ ๊ฐ€๊ฒฉ ๊ณ„์‚ฐ

  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ(price)๊ณผ ์ˆ˜๋Ÿ‰(quantity)์„ ์ž…๋ ฅ๋ฐ›๊ณ , ์ด๋น„์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
  • ๊ฐ€๊ฒฉ๊ณผ ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›์€ ํ›„์—๋Š” ์ด๋“ค์˜ ๊ณฑ์„ ์ถœ๋ ฅํ•˜์„ธ์š”. ์ถœ๋ ฅ ํ˜•ํƒœ๋Š” "์ด๋น„์šฉ: [๊ณฑํ•œ ๊ฒฐ๊ณผ]"์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • -1์„ ์ž…๋ ฅํ•˜์—ฌ ๊ฐ€๊ฒฉ ์ž…๋ ฅ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•˜์„ธ์š” (-1์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : 1000
๊ตฌ๋งคํ•˜๋ ค๋Š” ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์„ธ์š” : 3
์ด ๋น„์šฉ : 3000
์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•˜์„ธ์š” (-1์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : 2000
๊ตฌ๋งคํ•˜๋ ค๋Š” ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์„ธ์š” : 4
์ด ๋น„์šฉ : 8000
์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•˜์„ธ์š” (-1์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : -1
ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerWhileEx2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print("์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•˜์„ธ์š” (-1์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ) : ");
            int price = scanner.nextInt();

            if (price == -1) {
                System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
                break;
            }

            System.out.print("๊ตฌ๋งคํ•˜๋ ค๋Š” ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
            int quantity = scanner.nextInt();

            int totalCost = price * quantity;

            System.out.println("์ด ๋น„์šฉ : " + totalCost);
        }
    }
}

 

๋ฌธ์ œ์™€ ํ’€์ด 4

๋ฌธ์ œ - ์ž…๋ ฅํ•œ ์ˆซ์ž์˜ ํ•ฉ๊ณ„์™€ ํ‰๊ท 

  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ๊ทธ ์ˆซ์ž๋“ค์˜ ํ•ฉ๊ณ„์™€ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์‚ฌ์šฉ์ž๋Š” ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜๊ณ , ๋งˆ์ง€๋ง‰์—๋Š” -1์„ ์ž…๋ ฅํ•˜์—ฌ ์ˆซ์ž ์ž…๋ ฅ์„ ์ข…๋ฃŒํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์ˆซ์ž์˜ ์ž…๋ ฅ์ด ๋๋‚œ ํ›„์—๋Š”, ์ˆซ์ž๋“ค์˜ ํ•ฉ๊ณ„ sum๊ณผ ํ‰๊ท  average๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”. ํ‰๊ท ์€ ์†Œ์ˆ˜์  ์•„๋ž˜๊นŒ์ง€ ๊ณ„์‚ฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋‹ค์Œ ์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. ์ž…๋ ฅ์„ ์ค‘๋‹จํ•˜๋ ค๋ฉด -1์„ ์ž…๋ ฅํ•˜์„ธ์š” :
1
2
3
4
-1
์ž…๋ ฅํ•œ ์ˆซ์ž๋“ค์˜ ํ•ฉ๊ณ„ : 10
์ž…๋ ฅํ•œ ์ˆซ์ž๋“ค์˜ ํ‰๊ท  : 2.5

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerWhileEx3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int sum = 0;
        int count = 0;
        int num = 0;

        System.out.println("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. ์ž…๋ ฅ์„ ์ค‘๋‹จํ•˜๋ ค๋ฉด -1์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");

//        while (true) {
//
//            int num = scanner.nextInt();
//
//            if (num == -1) {
//                break;
//            }
//
//            sum += num;
//            count ++;
//        }

        while ((num = scanner.nextInt()) != -1) {
            sum += num;
            count ++;
        }

        double average = (double)sum / count;

        System.out.println("์ž…๋ ฅํ•œ ์ˆซ์ž๋“ค์˜ ํ•ฉ๊ณ„ : " + sum);
        System.out.println("์ž…๋ ฅํ•œ ์ˆซ์ž๋“ค์˜ ํ‰๊ท  : " + average);
    }
}

์œ„์˜ while๋ฌธ์ฒ˜๋Ÿผ ์ถ•์•ฝํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฌธ์ œ - ์ƒํ’ˆ ๊ตฌ๋งค

  • ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ƒํ’ˆ ์ •๋ณด(์ƒํ’ˆ๋ช…, ๊ฐ€๊ฒฉ, ์ˆ˜๋Ÿ‰)๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ์ด๋“ค์˜ ์ด ๊ฐ€๊ฒฉ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์‚ฌ ์šฉ์ž๋Š” ์—ฌ๋Ÿฌ ์ƒํ’ˆ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ฒฐ์ œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ์„ ์–ธ์ œ๋“ ์ง€ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์‚ฌ์šฉ์ž์—๊ฒŒ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์˜ต์…˜์„ ์ œ๊ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค: 1: ์ƒํ’ˆ ์ž…๋ ฅ, 2: ๊ฒฐ์ œ, 3: ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ. ์˜ต์…˜์€ ์ •์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ› ์œผ๋ฉฐ, ์˜ต์…˜์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ option ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ƒํ’ˆ ์ž…๋ ฅ ์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด, ์‚ฌ์šฉ์ž์—๊ฒŒ ์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ, ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›์œผ์„ธ์š”.
  • ๊ฒฐ์ œ ์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด, ์ด๋น„์šฉ์„ ์ถœ๋ ฅํ•˜๊ณ  ์ด๋น„์šฉ์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜์„ธ์š”. (์‚ฌ์šฉ์ž๊ฐ€ ์ด๋น„์šฉ์„ ํ™•์ธํ•˜๊ณ , ๊ฒฐ์ œ๋ฅผ ์™„๋ฃŒํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•ด ์ด๋น„์šฉ์„ ๋‹ค์‹œ0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.)
  • ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ ์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด, "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค."๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜์„ธ์š”. 
  • ์œ„์˜ ์„ธ ๊ฐ€์ง€ ์˜ต์…˜ ์™ธ์— ๋‹ค๋ฅธ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด, "์˜ฌ๋ฐ”๋ฅธ ์˜ต์…˜์„ ์„ ํƒํ•ด ์ฃผ์„ธ์š”."๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์‹œ

1: ์ƒํ’ˆ ์ž…๋ ฅ, 2: ๊ฒฐ์ œ, 3: ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
1
์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š”: ์Šคํ”„๋ง
์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”: 30000
๊ตฌ๋งค ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์„ธ์š”: 1
์ƒํ’ˆ๋ช…:์Šคํ”„๋ง ๊ฐ€๊ฒฉ:30000 ์ˆ˜๋Ÿ‰:1 ํ•ฉ๊ณ„:30000 
1: ์ƒํ’ˆ ์ž…๋ ฅ, 2: ๊ฒฐ์ œ, 3: ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
1
์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š”: JPA
์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”: 40000
๊ตฌ๋งค ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์„ธ์š”: 2
์ƒํ’ˆ๋ช…:JPA ๊ฐ€๊ฒฉ:40000 ์ˆ˜๋Ÿ‰:2 ํ•ฉ๊ณ„:80000
1: ์ƒํ’ˆ ์ž…๋ ฅ, 2: ๊ฒฐ์ œ, 3: ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ 
2
์ด ๋น„์šฉ: 110000
1: ์ƒํ’ˆ ์ž…๋ ฅ, 2: ๊ฒฐ์ œ, 3: ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ 
3
ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

์ •๋‹ต

package scanner.ex;

import java.util.Scanner;

public class ScannerWhileEx4 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int totalPrice = 0;

        while (true) {
            System.out.println("1: ์ƒํ’ˆ ์ž…๋ ฅ, 2: ๊ฒฐ์ œ, 3:ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
            int option = scanner.nextInt();
            scanner.nextLine();

            if (option == 1) {
                System.out.print("์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
                String name = scanner.nextLine();

                System.out.print("์ƒํ’ˆ์˜ ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
                int price = scanner.nextInt();

                System.out.print("๊ตฌ๋งค ์ˆ˜๋Ÿ‰์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
                int quantity = scanner.nextInt();

                int sum = price * quantity;

                totalPrice = totalPrice + sum;

                System.out.println("์ƒํ’ˆ๋ช… : " + name + " ๊ฐ€๊ฒฉ : " + price + " ์ˆ˜๋Ÿ‰ : " + quantity + " ํ•ฉ๊ณ„ : " + sum);
            } else if (option == 2) {
                System.out.println("์ด ๋น„์šฉ : " + totalPrice);
                totalPrice = 0;
            } else if (option == 3) {
                System.out.println("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
                break;
            } else {
                System.out.println("์˜ฌ๋ฐ”๋ฅธ ์˜ต์…˜์„ ์„ ํƒํ•ด์ฃผ์„ธ์š”.");
            }


        }
    }
}