| English | 释义 | 拼音 |
|---|---|---|
| assignment statement/əˈsaɪnmənt ˈsteɪtmənt/ | 赋值语句 | fù zhí yǔ jù |
赋值语句
- 赋值语句把一个值放进变量:
x = 5; - 单个
=表示“存入”,而不是“等于”。 - Java 先求右侧,再把结果存进左侧的变量。
count = count + 1;读取旧的count,加1,再存回去。
从右到左的流向
- 把
=想成一个指向左的箭头:total ← price * qty。 - 左侧的变量必须已经声明过(带类型)。
- 后来的赋值会覆盖旧值——盒子只保存最新的。
- 你可以在一行里声明并赋值:
int x = 5;

赋值将值复制进变量框;后续对一个变量的修改不影响另一个
用 Scanner 读取输入
- Java使用绑定到
System.in的**Scanner**对象读取用户输入。 Scanner sc = new Scanner(System.in);创建它。sc.nextInt()读一个int;sc.nextDouble()读一个double;sc.next()读一个单词。- 把读到的存进变量:
int age = sc.nextInt();
让读取匹配类型
- 用与你预期类型相匹配的读取方法。
nextInt()读整数,nextDouble()读小数,nextLine()读一整行。- 不匹配(如对文本 "hello" 用
nextInt())会造成运行时错误。 - 读进一个类型正确的变量,后面的代码才能工作。
= 是赋值,不是相等——而且它从右向左流。x = x + 1 不是矛盾;它计算 x + 1 再存回 x。(后面检验相等用 ==,是不同的运算符。)用 Scanner 时让方法匹配类型:nextInt() 读整数,读一个非数字会抛出运行时错误。
读取并更新一个分数:
int score = sc.nextInt();→score得到用户输入的任意数字。score = score + 10;→ 先算右侧score + 10,再存回去。- 如果用户输入
40,score现在是50。
赋值语句 x = value; 先求右侧,再把它存进左侧变量(= 是“存入”,而非相等)。用 Scanner 读输入:nextInt()、nextDouble() 或 next()/nextLine()——让方法匹配你预期的类型。
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.