[Java] Solution to puzzles

Joy๐ŸŒฑยท2022๋…„ 12์›” 25์ผ
0

๐Ÿงฉ Coding Challenges

๋ชฉ๋ก ๋ณด๊ธฐ
3/20
post-thumbnail
post-custom-banner

๐Ÿงฉ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ if-else๋ฌธ์œผ๋กœ ํ™€์ˆ˜ or ์ง์ˆ˜ ๊ตฌํ•˜๊ธฐ

๐Ÿ’โ€ 1~10 ์‚ฌ์ด์˜ ์ •์ˆ˜ ํ•œ ๊ฐœ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ™€์ˆ˜์ธ์ง€ ์ง์ˆ˜์ธ์ง€ ํ™•์ธํ•˜๊ณ ,
ํ™€์ˆ˜์ด๋ฉด "ํ™€์ˆ˜๋‹ค.", ํ™€์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด "์ง์ˆ˜๋‹ค."๋ผ๊ณ  ์ถœ๋ ฅํ•˜์„ธ์š”.
๋‹จ, 1~10 ์‚ฌ์ด์˜ ์ •์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ "๋ฐ˜๋“œ์‹œ 1 ~ 10 ์‚ฌ์ด์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

import java.util.Scanner;

public class Application1 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("1๊ณผ 10 ์‚ฌ์ด์˜ ์ •์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		int num = sc.nextInt();
		
		if(num > 0 && num < 11) {
			
			if(num % 2 == 1) {
				System.out.println("ํ™€์ˆ˜๋‹ค");
			} else {
				System.out.println("์ง์ˆ˜๋‹ค");
			}
			
		} else {
			System.out.println("๋ฐ˜๋“œ์‹œ 1 ~ 10 ์‚ฌ์ด์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.");
		}
	}
}

๐Ÿงฉ ์ฒด์ค‘, ํ‚ค๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ if-else if๋ฌธ์œผ๋กœ ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ BMI(์‹ ์ฒด์งˆ๋Ÿ‰์ง€์ˆ˜)๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ๊ณ„์‚ฐ๋œ ๊ฐ’์— ๋”ฐ๋ผ
์ €์ฒด์ค‘(20 ๋ฏธ๋งŒ)์ธ ๊ฒฝ์šฐ, "๋‹น์‹ ์€ ์ €์ฒด์ค‘ ์ž…๋‹ˆ๋‹ค."
์ •์ƒ์ฒด์ค‘(20์ด์ƒ 25๋ฏธ๋งŒ)์ธ ๊ฒฝ์šฐ, "๋‹น์‹ ์€ ์ •์ƒ์ฒด์ค‘ ์ž…๋‹ˆ๋‹ค."
๊ณผ์ฒด์ค‘(25์ด์ƒ 30๋ฏธ๋งŒ)์ธ ๊ฒฝ์šฐ, "๋‹น์‹ ์€ ๊ณผ์ฒด์ค‘ ์ž…๋‹ˆ๋‹ค."
๋น„๋งŒ(30์ด์ƒ)์ธ ๊ฒฝ์šฐ, "๋‹น์‹ ์€ ๋น„๋งŒ ์ž…๋‹ˆ๋‹ค."๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
BMI = ์ฒด์ค‘ / ํ‚ค * ํ‚ค

import java.util.Scanner;

public class Application2 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		
		System.out.println("๋‹น์‹ ์˜ ์ฒด์ค‘(kg)์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :  ");
		int kg = sc.nextInt();
		System.out.println("๋‹น์‹ ์˜ ํ‚ค(m)๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :  ");
		double cm = sc.nextDouble();
		
		double bmi = kg / (cm * cm);
		
		if(bmi < 20) {
			System.out.println("๋‹น์‹ ์€ ์ €์ฒด์ค‘ ์ž…๋‹ˆ๋‹ค.");
			
		} else if(bmi < 25) {
			System.out.println("๋‹น์‹ ์€ ์ •์ƒ์ฒด์ค‘ ์ž…๋‹ˆ๋‹ค.");
			
		} else if(bmi < 30) {
			System.out.println("๋‹น์‹ ์€ ๊ณผ์ฒด์ค‘ ์ž…๋‹ˆ๋‹ค.");
			
		} else if(bmi >= 30) {
			System.out.println("๋‹น์‹ ์€ ๋น„๋งŒ ์ž…๋‹ˆ๋‹ค.");
		}
	}
}

๐Ÿงฉ ์ •์ˆ˜ ๋ฐ ์—ฐ์‚ฐ๊ธฐํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ switch๋ฌธ์œผ๋กœ ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ

๐Ÿ’โ€ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ ,
์—ฐ์‚ฐ๊ธฐํ˜ธ(+, -, *, /, %)๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ํ•ด๋‹น ์—ฐ์‚ฐ์˜ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
๋‹จ, ์ค€๋น„๋œ ์—ฐ์‚ฐ๊ธฐํ˜ธ ์™ธ์˜ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ "์ž…๋ ฅํ•˜์‹  ์—ฐ์‚ฐ์€ ์—†์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค."๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.

import java.util.Scanner;

public class Application1 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("์ฒซ ๋ฒˆ์งธ ์ •์ˆ˜ : ");
		int num1 = sc.nextInt();
		System.out.println("๋‘ ๋ฒˆ์งธ ์ •์ˆ˜ : ");
		int num2 = sc.nextInt();
		System.out.println("์—ฐ์‚ฐ ๊ธฐํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		char point = sc.next().charAt(0);
		
		int result = 0;
		
		switch(point) {
		case '+' : result = num1 + num2;
			break;
		case '-' : result = num1 - num2;
			break;
		case '*' : result = num1 * num2;
			break;
		case '/' : result = num1 / num2;
			break;
		case '%' : result = num1 % num2;
			break;
		default : System.out.println("์ž…๋ ฅํ•˜์‹  ์—ฐ์‚ฐ์€ ์—†์Šต๋‹ˆ๋‹ค.ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค."); 
        	return; 
		}
			System.out.println(num1 + " " + point + " " + num2 + " = " + result);
	}
}

๐Ÿ“Œ Ref.

* return์„ ๋’ค์— ๋ง๋ถ™์—ฌ์ฃผ๋ฉด ์ƒ๋šฑ๋งž์€ ์—ฐ์‚ฐ๊ธฐํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์„œ "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค"๋ผ๋Š” ๋ฌธ๊ตฌ๊ฐ€ ๋– ๋„ 
  ์•„๋ž˜์˜ println()์‹์ด ํ•จ๊ป˜ ์ถœ๋ ฅ๋˜์ง€์•Š์Œ (๋ฉ”์†Œ๋“œ๋ฅผ return์œผ๋กœ ๊ฐ•์ œ ์ข…๋ฃŒํ–ˆ๊ธฐ ๋•Œ๋ฌธ)

๐Ÿ’ฌ Overall Comment

* ์ฒ˜์Œ์— return์„ ๋„ฃ์ง€์•Š์•„ ์—ฐ์‚ฐ๊ธฐํ˜ธ ๋Œ€์‹  ?๋ฅผ ๋„ฃ์œผ๋ฉด 1 ? 2 = 0 ์ด๋ผ๋Š” ์‹์ด ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ๋ฅผ ์•Œ๋ฆฌ๋Š” 
  ์ถœ๋ ฅ๋ฌธ๊ณผ ํ•จ๊ป˜ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ, ๋”์ด์ƒ์˜ ์ฝ”๋“œ ์ง„ํ–‰์ด ์—†์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ•์ œ ์ข…๋ฃŒ์‹œํ‚ค๋Š” 
  return์ด ์ž…๋ ฅ๋˜์–ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์› ๋‹ค.

๐Ÿงฉ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ switch๋ฌธ์œผ๋กœ ํ•ด๋‹น๋˜๋Š” ๊ฒƒ ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ ๊ณผ์ผ ์ด๋ฆ„("์‚ฌ๊ณผ", "๋ฐ”๋‚˜๋‚˜", "๋ณต์ˆญ์•„", "ํ‚ค์œ„") ์ค‘ ํ•œ ๊ฐ€์ง€๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹นํ•˜๋Š” ๊ฐ€๊ฒฉ์— ๋งž๊ฒŒ ์ƒํ’ˆ๋ช…๊ณผ ๊ฐ€๊ฒฉ์ด ์ถœ๋ ฅ๋˜๊ฒŒ ํ•˜์„ธ์š”.
๋‹จ, ๋ชฉ๋ก์— ์—†๋Š” ๊ณผ์ผ์„ ์ž…๋ ฅ ์‹œ "์ค€๋น„๋œ ์ƒํ’ˆ์ด ์—†์Šต๋‹ˆ๋‹ค." ์ถœ๋ ฅ ํ›„ ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ.

import java.util.Scanner;

public class Application2 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("๊ณผ์ผ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		String fruit = sc.nextLine();
		
		int price = 0;
		
		switch(fruit){
		case "์‚ฌ๊ณผ" : price = 1000;
			break;
		case "๋ฐ”๋‚˜๋‚˜" : price = 3000;
			break;
		case "๋ณต์ˆญ์•„" : price = 2000;
			break;
		case "ํ‚ค์œ„" : price = 5000;
			break;
		default : System.out.println("์ค€๋น„๋œ ์ƒํ’ˆ์ด ์—†์Šต๋‹ˆ๋‹ค."); 
            return; 
		}
		System.out.println(fruit + "์˜ ๊ฐ€๊ฒฉ์€ " + price + "์› ์ž…๋‹ˆ๋‹ค.");
	}
}

๐Ÿ’ฌ Overall Comment

* ์œ„ ๋ฌธํ•ญ๊ณผ ๋™์ผํ•˜๊ฒŒ case์— ์—†๋Š” ๊ฐ’์ด ์ž…๋ ฅ๋˜์—ˆ์„ ๊ฒฝ์šฐ, switch๋ฌธ ๋ฐ–์˜ ๋ฌธ์žฅ์„ ์ˆ˜ํ–‰ํ•˜์ง€์•Š๊ณ  ๊ฐ•์ œ ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 
  return์ด ๋ฐ˜๋“œ์‹œ ์ž…๋ ฅ๋˜์–ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ฐฐ์› ๋‹ค.

๐Ÿงฉ ๊ฐ ๊ณผ๋ชฉ ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ if else๋ฌธ์œผ๋กœ ํ•ฉ๊ฒฉ ๋ฐ ๋ถˆํ•ฉ๊ฒฉ ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ ๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™ ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ‰๊ท  ์ ์ˆ˜๊ฐ€ 60์  ์ด์ƒ์ด๋ฉด์„œ ๊ฐ ๊ณผ๋ชฉ์ด 40์  ์ด์ƒ์ธ ์กฐ๊ฑด์ด ๋งŒ์กฑํ•˜๋ฉด "ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค!"๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”.
๋‹จ, ํ•ฉ๊ฒฉ์ด ์•„๋‹Œ ๊ฒฝ์šฐ ๋ถˆํ•ฉ๊ฒฉ ์‚ฌ์œ ๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
ํ‰๊ท ์ ์ˆ˜ ๋ฏธ๋‹ฌ์ธ ๊ฒฝ์šฐ "ํ‰๊ท ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค."๋ผ๊ณ  ์ถœ๋ ฅํ•˜๊ณ ,
๊ณผ๋ชฉ๋‹น ๊ณผ๋ฝ ์ ์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ "xx ๊ณผ๋ชฉ์˜ ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ ์ž…๋‹ˆ๋‹ค." ์ถœ๋ ฅํ•˜์„ธ์š”.

package com.greedy.section01.conditional.level04.advanced;

import java.util.Scanner;

public class Application1 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("๊ตญ์–ด ์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		int kor = sc.nextInt();
		System.out.println("์˜์–ด ์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		int eng = sc.nextInt();
		System.out.println("์ˆ˜ํ•™ ์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ");
		int math = sc.nextInt();
		
		int avrg = (kor + eng + math)/3;
		
		if (avrg >= 60 && kor >= 40 && eng >= 40 && math >= 40 ) {
			System.out.println("ํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค!");
			
		} else if (avrg < 60) {
			System.out.println("ํ‰๊ท  ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.");
			  if (kor < 40) {
				System.out.println("๊ตญ์–ด ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.");
			} if (eng < 40) {
				System.out.println("์˜์–ด ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.");
			} if (math < 40) {
				System.out.println("์ˆ˜ํ•™ ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค.");
			}
			
		} else if (kor < 40) {
			System.out.println("๊ตญ์–ด ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค");
			
		} else if (eng < 40) {
			System.out.println("์˜์–ด ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค");
			
		} else if (math < 40) {
			System.out.println("์ˆ˜ํ•™ ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค");
		}
	}
}

๐Ÿ’ฌ Overall Comment

* ๋ฌธํ•ญ ์š”๊ฑด ์ค‘ ํ•ฉ๊ฒฉ์ด ์•„๋‹Œ ๊ฒฝ์šฐ, ๋ถˆํ•ฉ๊ฒฉ ์‚ฌ์œ ๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ '๋ชจ๋‘' ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค๋Š” ๋ถ€๋ถ„์ด ๋‚œ์ œ์˜€๋‹ค. 
  ๊ณ ๋ฏผ์„ ํ•˜๋‹ค๊ฐ€ 'ํ‰๊ท  ์ ์ˆ˜ ๋ฏธ๋‹ฌ๋กœ ๋ถˆํ•ฉ๊ฒฉ์ž…๋‹ˆ๋‹ค'๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒฝ์šฐ์—” ๊ฐ ๊ณผ๋ชฉ์˜ ์ ์ˆ˜๊ฐ€ ๋ฏธ๋‹ฌ์ธ์ง€๋„ ํ™•์ธ์„ 
  ํ•ด์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— else if๋ฌธ์— ๊ฐ๊ฐ if๋ฅผ ๋ถ™์—ฌ ๋ชจ๋“  ๋ถˆํ•ฉ๊ฒฉ ์‚ฌ์œ ๊ฐ€ ๋‚˜์˜ค๋„๋ก ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ–ˆ๋‹ค.

๐Ÿงฉ ์˜์—…์‚ฌ์›์˜ ์›”๊ธ‰์„ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

๐Ÿ’โ€ ์˜์—…์‚ฌ์›์˜ ์›”๊ธ‰์„ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
์›” ๊ธ‰์—ฌ์•ก๊ณผ ์›” ๋งค์ถœ์•ก์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ธ‰์—ฌ๋ฅผ ์‚ฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
์˜์—…์‚ฌ์›์€ ๋งค์ถœ์•ก ๋Œ€๋น„ ๋ณด๋„ˆ์Šค์œจ์— ๋ช…์‹œ๋œ ๋ณด๋„ˆ์Šค๋ฅผ ๊ธ‰์—ฌ ์™ธ์— ์ถ”๊ฐ€๋กœ ์ง€๊ธ‰๋ฐ›์Šต๋‹ˆ๋‹ค.
๋‹จ, ๋ณด๋„ˆ์Šค์œจ์€ ์ž…๋ ฅ ๋ฐ›์€ ์›” ๋งค์ถœ์•ก์— ๋น„๋ก€ํ•˜๋ฉฐ, ๊ณ„์‚ฐ๋œ ๋ณด๋„ˆ์Šค ๊ธˆ์•ก์„ ์›” ๊ธ‰์—ฌ์•ก์— ๋”ํ•˜์—ฌ ์ด ๊ธ‰์—ฌ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
๋ณด๋„ˆ์Šค์œจ์„ ์ ์šฉํ•˜์—ฌ ์ถœ๋ ฅ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ์ถœ๋ ฅ๋˜๋„๋ก ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด๋ณด์„ธ์š”.
์ด ๊ธ‰์—ฌ = ์›” ๊ธ‰์—ฌ + (๋งค์ถœ์•ก * ๋ณด๋„ˆ์Šค์œจ)

-- ๋งค์ถœ์•ก ๋Œ€๋น„ ๋ณด๋„ˆ์Šค์œจ --
   ๋งค์ถœ์•ก       ๋ณด๋„ˆ์Šค์œจ
 5์ฒœ๋งŒ์› ์ด์ƒ      5%
 3์ฒœ๋งŒ์› ์ด์ƒ      3%
 1์ฒœ๋งŒ์› ์ด์ƒ      1%
 1์ฒœ๋งŒ์› ๋ฏธ๋งŒ      0%

๐Ÿšฉ Example Output

======================
๋งค์ถœ์•ก : 20000000
๋ณด๋„ˆ์Šค์œจ : 1%
์›” ๊ธ‰์—ฌ : 3000000
๋ณด๋„ˆ์Šค ๊ธˆ์•ก : 200000
======================
์ด ๊ธ‰์—ฌ : 3200000
import java.util.Scanner;

public class Application2 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("์›” ๊ธ‰์—ฌ ์ž…๋ ฅ : ");
		int wage = sc.nextInt();
		System.out.println("๋งค์ถœ์•ก ์ž…๋ ฅ : ");
		int sales = sc.nextInt();
		
		double bonusPer = 0;
		
		if(sales >= 50000000) {
			bonusPer = 0.05;			
			
		} else if(sales >= 30000000) {
			bonusPer = 0.03;			
			
		} else if(sales >= 10000000) {
			bonusPer = 0.01;			
			
		} else
			bonusPer = 0.0;			
		
		double totalWage = (wage + sales * bonusPer);
		
		System.out.println("=================================");
		System.out.println("๋งค์ถœ์•ก : " + sales);
		System.out.println("๋ณด๋„ˆ์Šค์œจ : " + (int)(bonusPer * 100) + "%");
		System.out.println("์›” ๊ธ‰์—ฌ : " + wage);
		System.out.println("๋ณด๋„ˆ์Šค ๊ธˆ์•ก : " + (int)(sales * bonusPer));
		System.out.println("=================================");
		System.out.println("์ด ๊ธ‰์—ฌ : " + (int)totalWage);
	}
}

๐Ÿ’ฌ Overall Comment

* if-else๋ฌธ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ถ€๋ถ„์€ ์ˆœ์กฐ๋กœ์› ์œผ๋‚˜, bonusPer๊ฐ€ double๊ฐ’์ธ ์ ์—์„œ ๊ฒฐ๊ณผ๋ฅผ int๊ฐ’์œผ๋กœ ๋ฝ‘๊ธฐ ์œ„ํ•ด 
  ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜์„ ์‚ฌ์šฉํ•˜๋Š” ํŒŒํŠธ์—์„œ ๊ณ„์† ๋’ค์—‰์ผฐ์—ˆ๋‹ค. ์ตœ๋Œ€ํ•œ ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์‹ถ์—ˆ์œผ๋‚˜ ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜์œผ๋กœ 
  ์ธํ•œ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๋Š๋ผ 100% ๋ง˜์— ๋“ค๋„๋ก ์™„์„ฑํ•˜์ง€ ๋ชป ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ƒˆ๋กœ์šด ์•„์ด๋””์–ด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ 
  ํ•ด์ค€ ๋ฌธํ•ญ์ด์—ˆ๋‹ค.

๐Ÿ”‘ Used Keywords
if else switch return ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜

profile
Tiny little habits make me
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€