๋ฐ์ํ
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๋ฅผ ์์ ์ ์์ต๋๋ค.
๋ฐ์ํ
'๐ธ Algorithm > ๐ธ ๋ฐฑ์ค BaekJoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค][java][1008][A/B] (0) | 2023.08.20 |
---|---|
[๋ฐฑ์ค][java][10998][AรB] (0) | 2023.08.20 |
[๋ฐฑ์ค][java][1000][A+B] (0) | 2023.08.13 |
[๋ฐฑ์ค][java][2557][Hello World] (0) | 2023.08.13 |
๋ฐฑ์ค ์๊ณ ๋ฆฌ์ฆ์ ํ ๋ ์ฃผ์ ์ฌํญ (0) | 2023.08.06 |