JAVA ๊ณต๋ถ€ํ•˜๊ธฐ(5) ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป

66728 ๋‹จ์–ด JavaJava

JAVA ESSENTIAL Chaper 2 ์ž๋ฐ” ๊ธฐ๋ณธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต๋ฌธ์ œ
์ž๋ฐ” ์—์„ผ์…œ

1. Summary

1.(1) ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์˜ ๊ตฌ์กฐ

  • ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์€ ํด๋ž˜์Šค ์„ ์–ธ์œผ๋กœ ์‹œ์ž‘๋˜๋ฉฐ, ์ด ์•ˆ์—์„œ ๋ฉ”์†Œ๋“œ, ๋ณ€์ˆ˜, ์ƒ์ˆ˜ ๋“ฑ์„ ๋ชจ๋‘ ์„ ์–ธํ•œ๋‹ค. C/C++ ์—์„œ ๋งํ•˜๋Š” ์ „์—ญ ๋ณ€์ˆ˜, ์ „์—ญ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ์ด๋“ค์€ ๋ฐ˜๋“œ์‹œ ํด๋ž˜์Šค ๋‚ด์— ์บก์Šํ™”๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ํด๋ž˜์Šค๋Š” class ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ๋‹ค.

1.(2) ์ž๋ฐ”์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…

  • ์ž๋ฐ”์—๋Š” boolean, char, byte, short, int, long, float, double ๋“ฑ 8๊ฐœ์˜ ๊ธฐ๋ณธ ํƒ€์ž…์ด ์žˆ๋‹ค.
  • ์ž๋ฐ”์—๋Š” ํด๋ž˜์Šค์— ๋Œ€ํ•œ ๋ ˆํผ๋Ÿฐ์Šค, ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๋ ˆํผ๋Ÿฐ์Šค, ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋ ˆํผ๋Ÿฐ์Šค, ๋“ฑ 3๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๋ ˆํผ๋Ÿฐ์Šค ํƒ€์ž…์ด ์žˆ๋‹ค.
  • ํŽ˜ํผ๋Ÿฐ์Šค ํƒ€์ž…์€ C/C++ ์˜ ํฌ์ธํ„ฐ์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€์ง€๋Š” ์•Š๋Š”๋‹ค.
  • ๋ฌธ์ž์—ด์€ ๊ธฐ๋ณธ ํƒ€์ž…์ด ์•„๋‹ˆ๋ฉฐ ๋ ˆํผ๋Ÿฐ์Šค ํƒ€์ž…์ด๋‹ค.
  • ์ƒ์ˆ˜๋Š” final ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ดˆ๊นƒ๊ฐ’๊ณผ ํ•จ๊ป˜ ์„ ์–ธํ•˜๋ฉฐ, ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰ ์ค‘์— ๊ฐ’์ด ๋ฐ”๋€” ์ˆ˜ ์—†๋‹ค.
  • ์ž‘์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์—์„œ ํฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ํƒ€์ž… ๋ณ€ํ™˜์€ ์ž๋™์œผ๋กœ ์ผ์–ด๋‚œ๋‹ค.
  • ํฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…์—์„œ ์ž‘์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ํƒ€์ž… ๋ณ€ํ™˜์€ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•˜๋ฉฐ ๋ฐ์ดํ„ฐ ์†์‹ค์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

1.(3) ์ž๋ฐ”์—์„œ ํ‚ค ์ž…๋ ฅ

  • System.in์€ ํ‚ค๋ณด๋“œ๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ์ž…๋ ฅ๋ฐ›๋Š” ์ž๋ฐ”์˜ ํ‘œ์ค€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ ๊ฐ์ฒด๋กœ์„œ, ์ž…๋ ฅ๋œ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ”์ดํŠธ ์ •๋ณด๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
  • Scanner ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋ฉด ์ž๋ฐ”์—์„œ ํ‚ค ์ž…๋ ฅ์„ ์‰ฝ๊ฒŒ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. Scannner ํด๋ž˜์Šค๋Š” ์ž…๋ ฅ๋œ ํ‚ค๋ฅผ ์ˆซ์ž, ๋ฌธ์ž, ๋ฌธ์ž์—ด ๋“ฑ ์›ํ•˜๋Š” ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ œ๊ณตํ•œ๋‹ค.
  • Scanner ๊ฐ์ฒด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ์ž์— System.in์„ ๋„˜๊ฒจ์ฃผ๋Š” ์‹์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉฐ, System.in์œผ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋ฐ›์€ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์„ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ „๋‹ฌํ•œ๋‹ค.
Scanner scanner = new Scanner(System.in);
  • Scanner ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Scanner ํด๋ž˜์Šค์˜ ์ „์ฒด ๊ฒฝ๋กœ๋ช…์„ ์•Œ๋ ค์ฃผ๋Š” import java.util.Scanner;๋ฌธ์ด ํ•„์š”ํ•˜๋‹ค.

1.(4) ์—ฐ์‚ฐ์ž์™€ ์กฐ๊ฑด๋ฌธ

  • ์ž๋ฐ”์˜ ์—ฐ์‚ฐ์ž๋Š” C/C++์™€ ๊ฑฐ์˜ ๋™์ผํ•˜๋ฉฐ, ์‚ฐ์ˆ , ์ฆ๊ฐ, ๋น„๊ต, ์‹œํ”„ํŠธ, ๋น„ํŠธ, ๋…ผ๋ฆฌ, ์กฐ๊ฑด, ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  • ์ž๋ฐ”์˜ ์กฐ๊ฑด๋ฌธ์—๋Š” ๋‹จ์ˆœ if ๋ฌธ, if-else๋ฌธ, ๋‹ค์ค‘ if-else ๋ฌธ, ์ค‘์ฒฉ if-else ๋ฌธ, switch ๋ฌธ์ด ์žˆ๋‹ค.
  • switch ๋ฌธ์€ ๊ฐ’์— ๋”ฐ๋ผ ์—ฌ๋Ÿฌ ์‹คํ–‰ ๊ฒฝ๋กœ๋กœ ๋ถ„๊ธฐํ•˜๋Š” ๊ฒฝ์šฐ์— ์ ํ•ฉํ•˜๋‹ค.
  • switch ๋ฌธ์˜ case ์— break ๋ฌธ์„ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ์•„๋ž˜๋กœ ๊ณ„์† ์‹คํ–‰ํ•œ๋‹ค.
  • case ๋ฌธ์˜ ๊ฐ’์œผ๋กœ๋Š” ์ •์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด, ๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด, ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ณ€์ˆ˜๋‚˜ ์‹์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

2. ์ด๋ก  ๋ฌธ์ œ

2.(1) ์ž๋ฐ”์—์„œ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ๋Š”?

class

2.(2) ๋‹ค์Œ ์ค‘ ์‹๋ณ„์ž ์‚ฌ์šฉ์ด ์ž˜๋ชป๋œ ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ๊ณจ๋ผ๋ผ

int _i;
int %j;           //์ž˜๋ชป๋จ. '_' ์™€ '$'๋ฅผ ์ œ์™ธํ•œ ํŠน์ˆ˜ ๋ฌธ์ž๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
char ์•ˆ๋…•;
double 1var;      //์ž˜๋ชป๋จ. ๋ณ€์ˆ˜ ์ด๋ฆ„์— ์ˆซ์ž๋ฅผ ์“ธ ์ˆ˜ ์žˆ์ง€๋งŒ ์ฒซ๋ฒˆ์งธ๋กœ๋Š” ์˜ฌ ์ˆ˜ ์—†๋‹ค.
char student_ID;

2.(3) ๋‹ค์Œ ๊ฐ ํ•ญ๋ชฉ์ด ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋ผ

(1) intํ˜• ๋ณ€์ˆ˜ age -> int age;
(2) 0.25๋กœ ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋œ floatํ˜• ๋ณ€์ˆ˜ f -> float f = 0.25f;
(3) age ๊ฐ’๊ณผ f ๊ฐ’์„ ๋”ํ•œ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ double ํ˜• ๋ณ€์ˆ˜ d -> double d = age + f;
(4) ๋ฌธ์ž 'a' ๋กœ ์ดˆ๊ธฐํ™”๋œ charํ˜• ๋ณ€์ˆ˜ c -> char c = 'a';
(5) ์ž์‹ ์˜ ์ด๋ฆ„์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ๋ฌธ์ž์—ด ๋ณ€์ˆ˜ name -> String name = "KimKU";

2.(4) ๋‹ค์Œ ์ˆ˜์‹์˜ ๊ฒฐ๊ณผ ๊ฐ’๊ณผ ํƒ€์ž…์€?

(1) 67 + 12.8 -> 79.8 double
(2) 'c' + 1 -> 100 int
(3) 10/3 -> 3 int
(4) 10.0/3 -> 3.333... double
(5) 10==9 -> false boolean

2.(5) ๋‹ค์Œ ๋ฌธ์žฅ์„ ์กฐ๊ฑด์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ผ

(1) a๋Š” b๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค -> a >= b;
(2) a๋Š” b๋ณด๋‹ค ์ž‘๊ณ  c ๋ณด๋‹ค ํฌ๋‹ค -> a < b && a > c;
(3) a ๋”ํ•˜๊ธฐ 3์€ 10๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค -> a + 3 != 10;
(4) a๋Š” 10๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ b์™€ ๊ฐ™๋‹ค -> a > 10 || a == b;

2.(6) ๋‹ค์Œ ๋ฆฌํ„ฐ๋Ÿด ์ค‘์—์„œ case ๋ฌธ์˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์€?

(1) 10
(2) "A"
(3) 'A'
(4) 5.4 -> case ๋ฌธ์— ์ง€์ • ๊ฐ’์€ ์ •์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด, ๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด, ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด๋งŒ ํ—ˆ์šฉ

2.(7) 20๊ณผ 30์„ ๋”ํ•ด 50์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ ํ”„๋กœ๊ทธ๋žจ

 public class SampleProgram{
 	int i;
 	int j;
 	i = 20;
 	j = 30;
 	System.out.println(i+j);

 }

(1) ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์–ด๋–ค ์†Œ์Šค ํŒŒ์ผ ์ด๋ฆ„์œผ๋กœ ์ €์žฅ๋˜์–ด์•ผ ํ•˜๋Š”๊ฐ€?
๋‹ต: SampleProgram.java

(2) ์ด ํ”„๋กœ๊ทธ๋žจ์ด ์ •์ƒ์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋„๋ก ์ˆ˜์ •ํ•˜๋ผ

public class SampleProgram{
	public static void main(String[] args){
		int i;
		int j;
		i = 20;
		j = 30;
		System.out.println(i+j);
	}
}

2.(8) ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ ์ถœ๋ ฅ๋˜๋Š” ๋‚ด์šฉ์€?

switch(option){
	case 1: System.out.println("์˜ต์…˜ 1");
	case 2: System.out.println("์˜ต์…˜ 2");
	case 3: System.out.println("์˜ต์…˜ 3"); break;
	default: System.out.println("ํ•ด๋‹น ์—†์Œ");
}

(1) option์ด 1์ผ๋•Œ -> ์˜ต์…˜ 1 ์˜ต์…˜ 2 ์˜ต์…˜ 3
(2) option์ด 2์ผ๋•Œ -> ์˜ต์…˜ 2 ์˜ต์…˜ 3
(3) option์ด 3์ผ๋•Œ -> ์˜ต์…˜ 3
(4) option์ด 4์ผ๋•Œ -> ํ•ด๋‹น ์—†์Œ

2.(9) ๋‹ค์Œ ์กฐ๊ฑด๋ฌธ์„ ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•œ ์ค„๋กœ ์ž‘์„ฑ

if(j%2 == 0) i =10;
else i = 20;

๋‹ต: int i = (j%2 == 0) ? 10 : 20;

2.(10) ๋‹ค์Œ if-else ๋ฌธ์„ switch ๋ฌธ์œผ๋กœ ๋ฐ”๊พธ์–ด๋ผ

if(i == 1)
	System.out.println("!");
if(i == 2)
	System.out.println("@");
if(i == 3)
	System.out.printfln("#")
else
	System.out.println("*");
switch(i) {
    case 1 : System.out.println("1"); break;
    case 2 : System.out.println("@"); break;
    case 3 : System.out.println("#"); break;
    default : System.out.println("*");
}

3. ์‹ค์Šต ๋ฌธ์ œ

3.(1) ๋‘ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•ฉ์„ ๊ตฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

import java.util.Scanner;
public class prca {
  public static void main(String[] args){
      Scanner scanner = new Scanner(System.in);
      System.out.println("๋‘ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” >> ");

      int a, b;
      a = scanner.nextInt();
      b = scanner.nextInt();

      System.out.println(a+b);
  }
}

3.(2) ํ•œ ์ธต์˜ ๋†’์ด๊ฐ€ 5m, ๊ฑด๋ฌผ์ด ๋ช‡ ์ธต์ธ์ง€ ์ž…๋ ฅ๋ฐ›์•„ ๋†’์ด ์ถœ๋ ฅ

import java.util.Scanner;
public class prca {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        System.out.println("๋ช‡ ์ธต์ธ์ง€ ์ž…๋ ฅํ•˜์„ธ์š”");

        int a;
        a = scanner.nextInt();

        System.out.println(a*5 + "m ์ž…๋‹ˆ๋‹ค.");
    }
}

3.(3) x ๊ฐ’ ์ž…๋ ฅ๋ฐ›์•„ y=x^2-3x+7์„ ๊ณ„์‚ฐํ•ด y๊ฐ’ ์ถœ๋ ฅ

import java.util.Scanner;
public class prca {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        System.out.println("x ๊ฐ’์„ ์ž…๋ ฅํ•˜์„ธ์š”>> ");

        int x, y;
        x = scanner.nextInt();
        y = x*x -3*x + 7;

        System.out.println("x= " + x + ", y= " +y);

    }
}

3.(4) 2์ฐจ์› ํ‰๋ฉด์—์„œ ํ•˜๋‚˜์˜ ์ง์‚ฌ๊ฐํ˜•์€ ๋‘ ์ ์œผ๋กœ ํ‘œํ˜„๋œ๋‹ค. (50, 50)๊ณผ (100, 100)์˜ ๋‘ ์ ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์‚ฌ๊ฐํ˜•์ด ์žˆ๋‹ค๊ณ  ํ•˜์ž. ํ•œ ์ ์„ ๊ตฌ์„ฑํ•˜๋Š” ์ •์ˆ˜ x์™€ y ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๊ณ  ์ (x, y)๊ฐ€ ์ด ์ง์‚ฌ๊ฐํ˜• ์•ˆ์— ์žˆ๋Š”์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

import java.util.Scanner;
public class prca {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        System.out.println("์  (x, y)์˜ ์ขŒํ‘œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>");

        int x, y;

        x = scanner.nextInt();
        y = scanner.nextInt();

        if(x >= 50 && x <= 100 && y >= 50 && y <= 100)
            System.out.println("์ (" + x + ", " + y + ")์€ (50, 50)๊ณผ (100, 100)์˜ ์‚ฌ๊ฐํ˜• ๋‚ด์— ์žˆ์Šต๋‹ˆ๋‹ค.");
        else
            System.out.println("์ (" + x + ", " + y + ")์€ (50, 50)๊ณผ (100, 100)์˜ ์‚ฌ๊ฐํ˜• ๋‚ด์— ์—†์Šต๋‹ˆ๋‹ค.");
    }
}

3.(5) ๋…ผ๋ฆฌ ์—ฐ์‚ฐ AND์™€ OR์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ผ

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("๋…ผ๋ฆฌ ์—ฐ์‚ฐ์„ ์ž…๋ ฅํ•˜์„ธ์š”>>");

		boolean a = sc.nextBoolean();
		String operator = sc.next();
		boolean b = sc.nextBoolean();

		switch (operator) {
		case "AND":
			System.out.println(a&b);
			break;
		case "OR":
			System.out.println(a||b);
			break;

		default:
			System.out.println("์ž˜๋ชป๋œ ์ž…๋ ฅ์ž…๋‹ˆ๋‹ค.");
			break;
		}

	}	
}

3.(6) ๋ˆ์˜ ์•ก์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ 5๋งŒ์›๊ถŒ, ๋งŒ์›๊ถŒ, ์ฒœ์›๊ถŒ, 500์›, 100์›, 10์›, 1์› ๋™์ „์งœ๋ฆฌ ๊ฐ ๋ช‡ ๊ฐœ๋กœ ๋ณ€ํ™˜๋˜๋Š”์ง€ ์ถœ๋ ฅํ•˜๋ผ

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("๋ˆ์˜ ์•ก์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>");

		int money = sc.nextInt();
		
		System.out.print("์˜ค๋งŒ์›๊ถŒ"+money/50000+"๊ฐœ, ");
		money %= 50000;
		System.out.print("๋งŒ์›"+money/10000+"๊ฐœ, ");
		money %= 10000;
		System.out.print("์ฒœ์›"+money/1000+"๊ฐœ, ");
		money %= 1000;
		System.out.print("500์›"+money/500+"๊ฐœ, ");
		money %= 500;
		System.out.print("100์›"+money/100+"๊ฐœ, ");
		money %= 100;
		System.out.print("10์›"+money/10+"๊ฐœ, ");
		money %= 10;
		System.out.println("1์›"+money+"๊ฐœ");
	}	
}

3.(7) ํ•™์ ์ด A, B์ด๋ฉด "Excellent" C, D์ด๋ฉด "Good", ํ•™์ ์ด F ์ด๋ฉด "Bye" ๋ผ๊ณ  ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("๋ˆ์˜ ์•ก์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>");

		String grade = sc.next();
		
		switch (grade) {
		case "A": case "B":
			System.out.println("Excellent");
			break;
		case "C": case "D":
			System.out.println("Good");
			break;
		case "F": 
			System.out.println("Bye");
			break;
		default:
			System.out.println("์ž˜๋ชป ์ž…๋ ฅํ•˜์—ฟ์Šต๋‹ˆ๋‹ค.");
			break;
		}
	}	
}

3.(8) ์Œ๋ฃŒ์ˆ˜ ์ข…๋ฅ˜์™€ ์ž” ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๊ฐ€๊ฒฉ์„ ์•Œ๋ ค์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ. ์—์Šคํ”„๋ ˆ์†Œ๋Š” 2000์›, ์•„๋ฉ”๋ฆฌ์นด๋…ธ 2500์›, ์นดํ‘ธ์น˜๋…ธ 3000์›, ์นดํŽ˜๋ผ๋–ผ 3500์›

//if๋ฌธ                                                     
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("์ปคํ”ผ๋ฅผ ์ฃผ๋ฌธํ•˜์„ธ์š”>>");

		String coffee = sc.next();
		int Quantity = sc.nextInt();

		if(coffee.equals("์—์Šคํ”„๋ ˆ์†Œ"))
			System.out.println(2000*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
		else if(coffee.equals("์•„๋ฉ”๋ฆฌ์นด๋…ธ"))
			System.out.println(2500*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
		else if(coffee.equals("์นดํ‘ธ์น˜๋…ธ"))
			System.out.println(3000*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
		else if(coffee.equals("์นดํŽ˜๋ผ๋–ผ"))
			System.out.println(3500*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
		else
			System.out.println("์ž˜๋ชป ์ž…๋ ฅํ•˜์—ฟ์Šต๋‹ˆ๋‹ค.");

	}
}	

//switch๋ฌธ
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("์ปคํ”ผ๋ฅผ ์ฃผ๋ฌธํ•˜์„ธ์š”>>");

		String coffee = sc.next();
		int Quantity = sc.nextInt();
		
		switch (coffee) {
		case "์—์Šคํ”„๋ ˆ์†Œ":
			System.out.println(2000*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
			break;
		case "์•„๋ฉ”๋ฆฌ์นด๋…ธ": 
			System.out.println(2500*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
			break;
		case "์นดํ‘ธ์น˜๋…ธ": 
			System.out.println(3000*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
			break;
		case "์นดํŽ˜๋ผ๋–ผ": 
			System.out.println(3500*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
			break;
		default:
			System.out.println("์ž˜๋ชป ์ž…๋ ฅํ•˜์—ฟ์Šต๋‹ˆ๋‹ค.");
			break;
		}
	}	
}

3.9 369 ๊ฒŒ์ž„

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("1~99 ์‚ฌ์ด์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”>>");

		int num = sc.nextInt();
		
		int first = num/10;
		int second = num%10;
		int cnt = 0;
		
		if(first == 3 || first == 6 || first == 9) cnt++;
		if(second == 3 || second == 6 || second == 9) cnt++;
		
		switch (cnt) {
		case 0: System.out.println("๋ฐ•์ˆ˜์—†์Œ"); break;
		case 1: System.out.println("๋ฐ•์ˆ˜์ง"); break;
		case 2: System.out.println("๋ฐ•์ˆ˜์ง์ง"); break;
		default:
			break;
		}
	}
}	  

์‹ค์Šต๋ฌธ์ œ 8๋ฒˆ์„ ๋ฒˆํ˜•. ์ปคํ”ผ ๊ฐ€๊ฒฉ์€ ๋˜‘๊ฐ™์œผ๋ฉฐ ์—์Šคํ”„๋ ˆ์†Œ 10 ์ž” ์ด์ƒ ์ฃผ๋ฌธํ•˜๋ฉด ๊ฐ€๊ฒฉ์˜ 5%๋ฅผ ํ• ์ธ.

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("์ปคํ”ผ๋ฅผ ์ฃผ๋ฌธํ•˜์„ธ์š”>>");

		String coffee = sc.next();
		int Quantity = sc.nextInt();
		
		switch (coffee) {
		case "์—์Šคํ”„๋ ˆ์†Œ":
			int sum = 2000*Quantity;
			if(Quantity >= 10) sum *= 0.95;
			System.out.println(sum+"์›์ž…๋‹ˆ๋‹ค.");
			break;
		case "์•„๋ฉ”๋ฆฌ์นด๋…ธ": 
			System.out.println(2500*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
			break;
		case "์นดํ‘ธ์น˜๋…ธ": 
			System.out.println(3000*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
			break;
		case "์นดํŽ˜๋ผ๋–ผ": 
			System.out.println(3500*Quantity+"์›์ž…๋‹ˆ๋‹ค.");
			break;
		default:
			System.out.println("์ž˜๋ชป ์ž…๋ ฅํ•˜์—ฟ์Šต๋‹ˆ๋‹ค.");
			break;
		}
	}	
}                                                     

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ