๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โ˜•Java

[Java ์ดํ•ดํ•˜๊ธฐ] 15. Java๋กœ ๊ตฌ๊ตฌ๋‹จ ๋งŒ๋“ค๊ธฐ (2)

by Luvbit 2024. 5. 24.


 

๐Ÿฉท ์ด์ „์˜ Multiplication ํด๋ž˜์Šค๋Š” ์™„์„ฑ๋˜์—ˆ๊ณ , Main ํด๋ž˜์Šค๋ฅผ ์ด์–ด์„œ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 


 

Main ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์šฐ์„  ์‚ฌ์šฉ์ž์—๊ฒŒ ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด Scanner ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Scanner sc = new Scanner(System.in);

 

 

 

Multiplication ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด๋Š” ๊ตฌ๊ตฌ๋‹จ ๊ณ„์‚ฐ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

 

Multiplication multiple = new Multiplication();

 

 

๋นˆ ๋ฌธ์ž์—ด msg๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๊ตฌ๋‹จ์„ ๊ณ„์† ์ด์šฉํ•  ๊ฑด์ง€์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์–ป๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๊ทธ ํ›„, ๊ตฌ๊ตฌ๋‹จ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹œ์ž‘์„ ์•Œ๋ฆฝ๋‹ˆ๋‹ค.

 

String msg = "";
System.out.println("=====๊ตฌ๊ตฌ๋‹จ ํ”„๋กœ๊ทธ๋žจ ์ž‘๋™ ์‹œ์ž‘=====");

do {
	System.out.println("1x1 ํ˜•ํƒœ๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
	String input = sc.next();
	String[] parts = input.split("x");
	int x = Integer.parseInt(parts[0]);
	int y = Integer.parseInt(parts[1]);
			
// ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ด์–ด์„œ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

do-while ๋ฃจํ”„๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฃจํ”„๋Š” ์‚ฌ์šฉ์ž๊ฐ€ "exit"๋ฅผ ์ž…๋ ฅํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค.

Scanner๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ "1x1"ํ˜•ํƒœ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

 

๊ทธ ํ›„, "x'๋ฅผ ๊ธฐ์ค€์œผ๋กœ split ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฌธ์ž๋ฅผ ๋‚˜๋ˆˆ ํ›„, parts์˜ ๋ฐฐ์—ด์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

3x1์ธ ๊ฒฝ์šฐ, ์ด๋•Œ 0๋ฒˆ์งธ index์—๋Š” 1, 1๋ฒˆ์งธ index์—๋Š” 1์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

 


 

 

์—ฌ๊ธฐ์„œ String์˜ split์„ ํ†ตํ•ด ๋ฐฐ์—ด์— ๊ฐ’์„ ๋‹ด๋Š” ๋ฐฉ๋ฒ•์„ ๋ถ€๊ฐ€ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋จผ์ €, split ๋ฉ”์„œ๋“œ์˜ ๊ธฐ๋ณธ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

String[] parts = originalString.split(delimiter);

 

์—ฌ๊ธฐ์„œ originalString์€ ๋ถ„ํ• ํ•  ์›๋ณธ ๋ฌธ์ž์—ด์ด๊ณ , delimiter๋Š” ๋ถ„ํ•  ๊ธฐ์ค€์ด ๋˜๋Š” ๊ตฌ๋ถ„์ž์ž…๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๋˜๋Š” parts๋Š” ๋ถ„ํ• ๋œ ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š” ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.

์•„๋ž˜์— ๊ตฌ์ฒด์ ์ธ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค:

 

์˜ˆ์ œ ) ์ฝค๋งˆ๋กœ ๋ฌธ์ž์—ด ๋ถ„ํ• 

public class SplitExample {
    public static void main(String[] args) {
        String originalString = "apple,banana,orange";
        String delimiter = ",";
        String[] parts = originalString.split(delimiter);
        
        for (String part : parts) {
            System.out.println(part);
        }
    }
}

print >> 
apple
banana
orange

 

์ด์ฒ˜๋Ÿผ split ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ตฌ๋ถ„์ž๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด๋กœ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

์‹ค์ œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋„ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ด๋‹ˆ ๊ผญ ๊ธฐ์–ตํ•ด ์ฃผ์„ธ์š”!

 

 


 

 

 

	int result = multiple.getResult(x,y);
	System.out.println(x+"x"+y+"="+result);
			
	System.out.println("๊ณ„์†ํ•˜์‹ค๊ฑฐ๋ฉด y, ์ข…๋ฃŒ๋Š” exit๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
	msg = sc.next();
} while (!msg.equals("exit"));

 

๊ทธ๋Ÿฌ๋ฉด ์ด์ œ parts ๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ Index์—๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ฒซ ๋ฒˆ์งธ ์ž…๋ ฅํ•œ ์ˆซ์ž,

parts ๋ฐฐ์—ด์˜ 1๋ฒˆ์งธ Index์—๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋‘ ๋ฒˆ์งธ ์ž…๋ ฅํ•œ ์ˆซ์ž๊ฐ€ ์กด์žฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ ํ›„ Multiplication ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์ธ getResult ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ, ํ•ด๋‹น ๊ตฌ๊ตฌ๋‹จ์˜ ๊ฒฐ๊ด๊ฐ’์„ ์ €์žฅ ํ›„์— ์ถœ๋ ฅํ•ด ์ค๋‹ˆ๋‹ค.

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

 

 

 

์‹คํ–‰ํ•˜์‹œ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2์ฐจ์› ๋ฐฐ์—ด์„ ํ™œ์šฉํ•˜์—ฌ ๊ตฌ๊ตฌ๋‹จ ๋งŒ๋“ค๊ธฐ ์™„์„ฑ์ž…๋‹ˆ๋‹ค!

 

 

 

 

 

 

์•„๋ž˜๋Š” ์ „์ฒด ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

package multiplication;

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Multiplication multiple = new Multiplication();
		String msg = "";
		System.out.println("=====๊ตฌ๊ตฌ๋‹จ ํ”„๋กœ๊ทธ๋žจ ์ž‘๋™ ์‹œ์ž‘=====");
		
		
		do {
			System.out.println("1x1 ํ˜•ํƒœ๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
			String input = sc.next();
			String[] parts = input.split("x");
            int x = Integer.parseInt(parts[0]);
            int y = Integer.parseInt(parts[1]);
			
			int result = multiple.getResult(x,y);
			System.out.println(x+"x"+y+"="+result);
			
			System.out.println("๊ณ„์†ํ•˜์‹ค๊ฑฐ๋ฉด y, ์ข…๋ฃŒ๋Š” exit๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
			msg = sc.next();
		} while (!msg.equals("exit"));

	}
}

 

 

 

๊ตฌ๊ตฌ๋‹จ ๋งŒ๋“ค๊ธฐ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ๊ธฐ๋ณธ์ ์ธ ๋ฐฐ์—ด ํ™œ์šฉ์„ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ๊ธฐ์ดˆ ์ง€์‹์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ํฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ํฌ์ŠคํŒ…์œผ๋กœ ์ฐพ์•„๋ต™๊ฒ ์Šต๋‹ˆ๋‹ค
๊ถ๊ธˆํ•˜์‹  ์ ์€ ๋Œ“๊ธ€์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š”!
๐Ÿฉต๐Ÿฐ๐Ÿฉต