Assignment and Input · 赋值与输入
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| assignment statement/əˈsaɪnmənt ˈsteɪtmənt/ | 赋值语句 | fù zhí yǔ jù |
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.
赋值语句
- 赋值语句把一个值放进变量:
x = 5; - 单个
=表示“存入”,而不是“等于”。 - Java 先求右侧,再把结果存进左侧的变量。
count = count + 1;读取旧的count,加1,再存回去。
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;
从右到左的流向
- 把
=想成一个指向左的箭头:total ← price * qty。 - 左侧的变量必须已经声明过(带类型)。
- 后来的赋值会覆盖旧值——盒子只保存最新的。
- 你可以在一行里声明并赋值:
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();
用 Scanner 读取输入
- 要读取用户输入,Java 用一个绑定到
System.in的Scanner对象。 Scanner sc = new Scanner(System.in);创建它。sc.nextInt()读一个int;sc.nextDouble()读一个double;sc.next()读一个单词。- 把读到的存进变量:
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.
让读取匹配类型
- 用与你预期类型相匹配的读取方法。
nextInt()读整数,nextDouble()读小数,nextLine()读一整行。- 不匹配(如对文本 "hello" 用
nextInt())会造成运行时错误。 - 读进一个类型正确的变量,后面的代码才能工作。
= 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.
= 是赋值,不是相等——而且它从右向左流。x = x + 1 不是矛盾;它计算 x + 1 再存回 x。(后面检验相等用 ==,是不同的运算符。)用 Scanner 时让方法匹配类型:nextInt() 读整数,读一个非数字会抛出运行时错误。
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.
读取并更新一个分数:
int score = sc.nextInt();→score得到用户输入的任意数字。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.
赋值语句 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... · 在 Java 中,单个 = 号表示……
= is assignment; == tests equality. · = 是赋值;== 才检验相等。
score is 40, then score = score + 10; runs. What is score now? · score 是 40,然后运行 score = score + 10;。现在 score 是多少?
Right side 40+10 = 50 is stored back into score. · 右侧 40+10 = 50 被存回 score。
Which Scanner method reads a whole number? · 哪个 Scanner 方法读取一个整数?
nextInt() reads an int; nextDouble() reads a decimal. · nextInt() 读 int;nextDouble() 读小数。
Java evaluates the right side of an assignment before storing into the left variable. · Java 在存入左侧变量之前,先求赋值语句的右侧。
Right side first, then store — that's how x = x + 1 works. · 先右侧,再存储——这就是 x = x + 1 的原理。
Calling nextInt() when the user types the word 'hello' causes a run-time error. · 当用户输入单词 'hello' 时调用 nextInt() 会造成运行时错误。
The method expects an int; text mismatches it. · 该方法期望一个 int;文本与它不匹配。