Gán và Nhập liệu
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| assignment statement/əˈsaɪnmənt ˈsteɪtmənt/ | câu gán |
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;

Assignment copies a value into a variable's box; later changes to one variable do not affect the other
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.
Vế phải trước, sau đó lưu
Mỗi phép gán đánh giá vế phải, sau đó ghi đè lên ô.
Trong Java, dấu = đơn lẻ có nghĩa là...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
= là gán; == kiểm tra sự bằng nhau.
score là 40, sau đó score = score + 10; chạy. Vậy score hiện tại là gì?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Vế phải 40+10 = 50 được lưu lại vào score.
Phương thức Scanner nào đọc số nguyên?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
nextInt() đọc int; nextDouble() đọc số thập phân.
Java đánh giá vế phải của phép gán trước khi lưu vào biến bên trái.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Vế phải trước, sau đó lưu — đó là cách x = x + 1 hoạt động.
Gọi nextInt() khi người dùng nhập từ 'hello' gây ra lỗi thời gian chạy.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Phương thức yêu cầu int; văn bản không khớp với nó.