๐Ÿ•ธ Algorithm/๐Ÿ•ธ ๋ฐฑ์ค€ BaekJoon

[๋ฐฑ์ค€][java][1001][A-B]

๋‰ด์ด NUEY 2023. 8. 15. 16:39
๋ฐ˜์‘ํ˜•

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

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

		int firstNumber = scanner.nextInt();
		int secondNumber = scanner.nextInt();
		
		System.out.print(firstNumber - secondNumber);
	}
}

๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€์–ด๋ณด๋ฉด ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

 

 

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		Main aMinusB = new Main();
        
		List<Integer> numbersList = new ArrayList<Integer>();
		
		numbersList.add(scanner.nextInt());
		numbersList.add(scanner.nextInt());
		
		if (numbersList.size() == 2) aMinusB.printNumber(numbersList);
	}
	
	public void printNumber(List<Integer> numbers) {
		int firstNumber  = numbers.get(0) == null ? 0 : numbers.get(0);
		int secondNumber = numbers.get(1) == null ? 0 : numbers.get(1);
        
        System.out.printf("%d", firstNumber - secondNumber);
	}
}

์ถœ๋ ฅ๋ถ€๋ถ„์„ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด๋ณด๋ฉด ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

 

  • ์ž…๋ ฅ๊ฐ’์„ Integerํ˜•์œผ๋กœ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ scanner.nextInt()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
  • List์˜ ๊ฐ’์ด ์žˆ๋Š” ์ง€ ๊ณ„์†ํ•ด์„œ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    ์•ˆ๊ทธ๋Ÿผ ์—๋””ํ„ฐ์—์„œ๋Š” ๋Œ์•„๊ฐ€๋„ ์ฑ„์ ์‹œ java.util.NoSuchElementException ์—๋Ÿฌ๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค.
  • ์ฐธ๊ณ ๋กœ printNumber()ํ•จ์ˆ˜์—์„œ void ์•ž์— static์„ ๋ถ™์ด๋ฉด ํด๋ž˜์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.
  • printf() ์ถœ๋ ฅ์‹œ Integerํ˜•์„ ์ถœ๋ ฅํ•˜๊ธฐ์— %d๋ฅผ ์•ž์— ์ ์—ˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•