Java์—์„œ ๋‘ ์ˆซ์ž์˜ min()์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

Java๋Š” ๊ด‘๋ฒ”์œ„ํ•˜๊ณ  ํŽธ๋ฆฌํ•œ ์ž‘์—…์„ ์œ„ํ•ด " java.lang.Math " ๋กœ ์•Œ๋ ค์ง„ ์‹œ์Šคํ…œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค . ์‚ผ๊ฐ๋ฒ•์—์„œ ๋กœ๊ทธ ํ•จ์ˆ˜์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์œผ๋กœ ์ธํ•ด ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž์˜ ์ตœ์†Œ/์ตœ๋Œ€ ๋˜๋Š” ์ ˆ๋Œ€๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Math.min() ๋ฉ”์„œ๋“œ

๋‹ค์Œ์€ ๋ฉ”์„œ๋“œ์˜ ์ผ๋ฐ˜์ ์ธ ํ‘œํ˜„์ž…๋‹ˆ๋‹ค.

Math.min(a, b)
์ด ํ•จ์ˆ˜๋Š” ๋™์ผํ•œ ์œ ํ˜• int , long , float ๋˜๋Š” double ์˜ ๋‘ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค . ํšจ๊ณผ์ ์ธ ์‚ฌ์šฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด Math.min() ๋ฉ”์„œ๋“œ ์˜ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค . IDE์—์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ถœ๋ ฅ์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ 1


package com.math.min.core
public class MathMinMethod {
	public static void main(String[] args) {

		int leenasAge = 10;
		int tiffanysAge = 15;
		// example of min () function
            int min = Math.min(leenasAge, tiffanysAge);

		System.out.print("Who's the younger sister? ");
		if (min < tiffanysAge)
			System.out.print("Leena ------- Age " + leenasAge);
		else
			System.out.print("Tiffany ------- Age " + tiffanysAge);
	}
}

์‚ฐ์ถœ

์—ฌ๋™์ƒ์€ ๋ˆ„๊ตฌ์ž…๋‹ˆ๊นŒ? ๋ฆฌ๋‚˜ ------- 10์„ธ

์„ค๋ช…

8ํ–‰์—์„œ int min = Math.min(leenasAge, tiffanysAge); int min์€ min() ํ•จ์ˆ˜ ๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ์ตœ์†Œ ์ˆ˜๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค . ๋‚˜์ค‘์— ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์€ ํ˜•์ œ์˜ ๋‚˜์ด๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

์˜ˆ 2


package com.math.min.core;
public class MathMinMethod {
	public static void main(String[] args) {

		double berriesSoldInKg = 15.6;
		double cherriesSoldInKg = 21.3;
            // example of min () function
		double min = Math.min(berriesSoldInKg, cherriesSoldInKg);

		System.out.println("What's the minimum weight sold?");
		if (min != cherriesSoldInKg)
			System.out.print("Berries: " + berriesSoldInKg + " kg");
		else
			System.out.print("Cherries: " + cherriesSoldInKg +"kg");
	}
}

์‚ฐ์ถœ

ํŒ๋งค๋˜๋Š” ์ตœ์†Œ ๋ฌด๊ฒŒ๋Š” ์–ผ๋งˆ์ž…๋‹ˆ๊นŒ? ๋”ธ๊ธฐ: 15.6kg

์„ค๋ช…

8ํ–‰์—์„œ double min = Math.min(berriesSoldInKg, cherriesSoldInKg); ์ด์ค‘ "min"์€ ๋‘ ๊ฐ€์ค‘์น˜ ์ค‘ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ๋‘ ๋ฐฐ(kg ๋‹จ์œ„์˜ ์–‘)๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‘ ๊ณผ์ผ์˜ ์ตœ์†Œ๊ฐ’์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ์–ด๋–ค ๊ฒฝ์šฐ์—๋„ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

์ด์ œ Math.min() ๋ฉ”์„œ๋“œ์˜ ํ•„์š”์„ฑ๊ณผ ํšจ์œจ์„ฑ์„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์งˆ๋ฌธ์ด๋‚˜ ํ˜ผ๋™์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ด ๋ฌธ์„œ๋ฅผ ๋‹ค์‹œ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. ๊ณ„์† ์„ฑ์žฅํ•˜๊ณ  ์—ฐ์Šตํ•˜์„ธ์š”!