https://www.acmicpc.net/problem/25304
์์๋ ๋ฌธ์ ๋งํฌ!
์๋๋ ๋ฌธ์ ์บก์ณ!
Java ๋ก ํ์๋ค!
๋๋ง ๋๋ฌด ์ด๋ ต๊ฒ ์๊ฐํ๋ ใ ๋ง์ ์ฌ๋๋ค์ ์์ฒญ ์ฝ๊ฒ ํผ ๊ฒ ๊ฐ๋ค.
๋๋ ์ค๊ฐ์ค๊ฐ ๋ฌธ์ฅ๋ ๋ฃ์๋ค
import java.util.Scanner;
public class cal {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Your total price : ");
int total = input.nextInt();
System.out.println("How many kind of things did you buy? : ");
int kind = input.nextInt();
//์์์ ๋๋ 4ํ๊ณ enter ๋ฅผ ์น๋ฉด, enter ("\n") ๊ฐ ๊ทธ ๋ค์ input.nextLine() ๋ฉ์๋๋ ๋ฒํผ์ ๋จ์ ์๋ ์ค๋ฐ๊ฟ ๋ฌธ์ \n์ ์ฝ์ต๋๋ค. ๊ทธ ๊ฒฐ๊ณผ ๋น ๋ฌธ์์ด ""์ด abstr[0]์ ์ ์ฅ๋ฉ๋๋ค.
// ๊ทธ๊ฑธ ๋ฐฉ์งํ๊ธฐ ์ํด ์๋ ๋ถ๋ถ์์ ์๋์ ๊ฐ์ด ๋จ์ ์ค๋ฐ๊ฟ ๋ฌธ์๋ฅผ ์๋น
input.nextLine();
System.out.println("Then let me know all of the price and the EA" );
String[] abstr = new String[kind];
for (int i = 0; i<kind; i++) {
abstr[i] = input.nextLine();
String[][] absplitstr = {abstr[i].split("\\s")};
int[] absplitint = {Integer.parseInt(absplitstr[0][0]), Integer.parseInt(absplitstr[0][1])};
total -= (absplitint[0]*absplitint[1]);
}
if (total == 0) {
System.out.println("Yes!");
} else {
System.out.println("no!");
}
}
์ฐธ๊ณ ๋ก, ๋ด๊ฐ 17๋ฒ line ์ ์ฝ๋๋ฅผ ๋นผ๋จน์์ ๋, abstr[0] = ""; ์ด๋ ๊ฒ ๋น์นธ์ด ๋ค์ด๊ฐ๋ฒ๋ฆฌ๋ ๋ฌธ์ ๊ฐ ์๊ฒผ๊ณ , ๊ทธ ์ด์ ๋ฅผ ๋ชฐ๋๋๋ฐ, Chat gpt ์ ์๋์ด ์น์ ํ๊ฒ ๊ทธ ์ด์ ๋ฅผ ์๋ ค์คฌ๋ค.
์๋๋ gpt ๋ต๋ณ!
abstr[0]์ ๋น ๋ฌธ์์ด ""์ด ๋ค์ด๊ฐ๋ ์ด์ ๋ Scanner ๊ฐ์ฒด์ nextLine() ๋ฉ์๋๊ฐ ์ด์ ์ ๋ ฅ์์ ๋จ์ ์ค๋ฐ๊ฟ ๋ฌธ์(\n)๋ฅผ ์ฝ์ด๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์ ๋๋ค.
์๋์์ ์ด ์ํฉ์ ๋จ๊ณ๋ณ๋ก ์ค๋ช ํ ๊ฒ์.
์ฝ๋ ๋์ ๊ณผ์
- total ๊ฐ ์ ๋ ฅ: ์ฌ์ฉ์๊ฐ input.nextInt()๋ฅผ ํตํด ์ ์ 260000์ ์ ๋ ฅํฉ๋๋ค. ์ด๋ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๊ฐ ๋ค์ Enter ํค๋ฅผ ๋๋ฅด๋ฉด, ์ซ์ 260000 ๋ค์ \n (์ค๋ฐ๊ฟ ๋ฌธ์)์ด ์ ๋ ฅ ๋ฒํผ์ ๋จ์ ์๊ฒ ๋ฉ๋๋ค.
- kind ๊ฐ ์ ๋ ฅ: ๋ง์ฐฌ๊ฐ์ง๋ก, input.nextInt()๋ฅผ ํตํด ์ ์ 4๋ฅผ ์ ๋ ฅํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ \n์ด ๋ค์ ์ ๋ ฅ ๋ฒํผ์ ๋จ์ ์๊ฒ ๋ฉ๋๋ค.
- ์ฒซ ๋ฒ์งธ nextLine() ํธ์ถ: ๋ฐ๋ณต๋ฌธ ์์์ abstr[0] = input.nextLine();์ด ์คํ๋๋ฉด, input.nextLine() ๋ฉ์๋๋ ๋ฒํผ์ ๋จ์ ์๋ ์ค๋ฐ๊ฟ ๋ฌธ์ \n์ ์ฝ์ต๋๋ค. ๊ทธ ๊ฒฐ๊ณผ ๋น ๋ฌธ์์ด ""์ด abstr[0]์ ์ ์ฅ๋ฉ๋๋ค.
- ์ดํ nextLine() ํธ์ถ: ๊ทธ ๋ค์ ๋ฐ๋ณต์์ input.nextLine()์ด ํธ์ถ๋ ๋๋ง๋ค ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๊ฐ์ ์ ๋๋ก ์ฝ๊ฒ ๋ฉ๋๋ค.
ํด๊ฒฐ ๋ฐฉ๋ฒ
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด nextInt() ๋ค์์ nextLine()์ ํ ๋ฒ ํธ์ถํ์ฌ ์ ๋ ฅ ๋ฒํผ์ ๋จ์ ์๋ ์ค๋ฐ๊ฟ ๋ฌธ์๋ฅผ ์๋นํด์ฃผ๋ฉด ๋ฉ๋๋ค.
์ด์ ์์ผ๋ก ์ต๊ด์ ๋ค์ด์!
๊ผญ! nextInt(); ๋ฅผ ์ฐ๋ฉด, ์๋์ input.nextLine(); ์ ์จ์ค์ "\n" ์ ์๋นํด์ฃผ์!
int num = input.nextInt();
input.nextLine();
๋๋ฌด ์ด๋ ต๊ฒ ํ์๋? ์ถ์ ๋ฐฑ์ค ๋ฌธ์ java ํ์ด ๋!