Assignment and Input
แนะนำใหม่
15:00
การใช้วัตถุและวิธีการ
นี่คือโปรแกรม Java สองตัว. ตัวแรกขาดเครื่องหมายจุดกึ่งวรรค. ตัวที่สองหารเจ็ดด้วยสองและเก็บคำตอบลงในจำนวนเต็ม. คอมไพเลอร์หยุดการ…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| assignment statement/əˈsaɪnmənt ˈsteɪtmənt/ | การกำหนดค่า |
คำสั่งการกำหนดค่า
- An assignment statement ใส่ค่าเข้าไปในตัวแปร:
x = 5; =เดียวหมายถึง "เก็บลงใน" ไม่ใช่ "เท่ากับ"- Java ประเมิน ด้านขวาก่อน แล้วเก็บผลลัพธ์ลงในตัวแปรด้านซ้าย
count = count + 1;อ่านค่าเก่าcount, บวก1, และเก็บกลับ
กระบวนการจากขวาไปซ้าย
- ให้มอง
=เป็นลูกศรชี้ไปทางซ้าย:total ← price * qty - ตัวแปรทางซ้ายต้องถูก ประกาศไว้ก่อน (พร้อมประเภท)
- การกำหนดค่าครั้งใหม่จะ ทับค่าเก่า — กล่องจะเก็บเพียงค่าล่าสุดเท่านั้น
- คุณสามารถประกาศและกำหนดค่าในบรรทัดเดียวได้:
int x = 5;

การกำหนดค่าคัดลอกค่าลงในกล่องตัวแปร; การเปลี่ยนแปลงในภายหลังของตัวแปรหนึ่งจะไม่ส่งผลต่ออีกตัวแปรหนึ่ง
การอ่านข้อมูลเข้าด้วย Scanner
- ในการอ่านข้อมูลผู้ใช้ Java ใช้
Scannerobject ที่ผูกกับSystem.in Scanner sc = new Scanner(System.in);สร้างมันsc.nextInt()reads anint;sc.nextDouble()adouble;sc.next()one word.- เก็บสิ่งที่อ่านได้ในตัวแปร:
int age = sc.nextInt();
จับคู่วิธีการอ่านให้ตรงกับประเภท
- ใช้วิธีการอ่านที่ตรงกับประเภทที่คุณคาดหวัง
nextInt()สำหรับจำนวนเต็ม,nextDouble()สำหรับทศนิยม,nextLine()สำหรับทั้งบรรทัด- ความไม่ตรงกัน (เช่น
nextInt()กับข้อความ "hello") ทำให้เกิด run-time error - อ่านลงในตัวแปรที่มีประเภทที่ถูกต้องเพื่อให้โค้ดส่วนอื่นทำงานได้
= 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.
การอ่านและอัปเดตคะแนน:
int score = sc.nextInt();→scoreจะได้whatever number ผู้ใช้พิมพ์มาscore = score + 10;→ ด้านขวาscore + 10ถูกคำนวณ, แล้วเก็บกลับ- ถ้าผู้ใช้พิมพ์
40,scoreตอนนี้คือ50
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.
Right side first, then store
Each assignment evaluates the right side, then overwrites the box.
In Java, the single = sign means...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
= is assignment; == tests equality.
score is 40, then score = score + 10; runs. What is score now?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Right side 40+10 = 50 is stored back into score.
Which Scanner method reads a whole number?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
nextInt() reads an int; nextDouble() reads a decimal.
Java evaluates the right side of an assignment before storing into the left variable.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Right side first, then store — that's how x = x + 1 works.
Calling nextInt() when the user types the word 'hello' causes a run-time error.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The method expects an int; text mismatches it.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้