| English | 의미 |
|---|---|
| assignment statement/əˈsaɪnmənt ˈsteɪtmənt/ | 대입 문장 |
The assignment statement
- An assignment statement 赋值语句 puts a value into a variable:
x = 5; - The single
=means "store into," not "is equal to." - Java evaluates the right side first, then stores the result in the left-side variable.
count = count + 1;reads the oldcount, adds1, and stores it back.
Right-to-left flow
- Think of
=as a left-pointing arrow:total ← price * qty. - The variable on the left must already be declared (with a type).
- A later assignment overwrites the old value — the box holds only the newest.
- You can declare and assign in one line:
int x = 5;
Reading input with Scanner
- To read user input, Java uses a
Scannerobject tied toSystem.in. Scanner sc = new Scanner(System.in);creates it.sc.nextInt()reads anint;sc.nextDouble()adouble;sc.next()one word.- Store what you read in a variable:
int age = sc.nextInt();
Match the read to the type
- Use the reading method that matches the type you expect.
nextInt()for whole numbers,nextDouble()for decimals,nextLine()for a whole line.- A mismatch (e.g.
nextInt()on the text "hello") causes a run-time error. - Read into a correctly-typed variable so the rest of the code works.
= is assignment, not equality — and it flows right-to-left. x = x + 1 is not a contradiction; it computes x + 1 and stores it back in x. (Testing equality later uses ==, a different operator.) With Scanner, match the method to the type: nextInt() reads an integer, and reading a non-number throws a run-time error.
Reading and updating a score:
int score = sc.nextInt();→scoregets whatever number the user typed.score = score + 10;→ right sidescore + 10is computed, then stored back.- If the user typed
40,scoreis now50.
An assignment statement x = value; evaluates the right side first, then stores it in the left variable (= is "store into," not equality). Read input with a Scanner: nextInt(), nextDouble(), or next()/nextLine() — match the method to the type you expect.
오른쪽을 먼저 평가한 뒤 저장합니다
각 대입 문장은 오른쪽을 평가한 후 해당 박스의 값을 덮어씁니다.
Java에서 단일 = 부호는 ...를 의미합니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
=는 대입이며, ==는 동일성(equality)을 테스트합니다.
score가 40일 때 score = score + 10를 실행합니다.此时的 score는?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
오른쪽 40+10 = 50을 다시 score에 저장합니다.
정수 전체를 읽는 Scanner 메서드는?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
nextInt()는 int를 읽으며, nextDouble()는 소수점 숫자를 읽습니다.
Java는 변수에 저장하기 전에 대입문의 오른쪽을 먼저 평가합니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
오른쪽을 먼저 평가하고 저장 — 이것이 x = x + 1이 작동하는 방식입니다.
사용자가 'hello'라고 입력했을 때 nextInt()를 호출하면 실행 중 오류가 발생합니다.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
메서드는 int를 기대하지만 텍스트가 일치하지 않습니다.