Присваивание и ввод
Введено| 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;

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.
Сначала правая сторона, затем сохранение
Каждое присваивание вычисляет правую сторону, затем перезаписывает ячейку.
В Java одиночный знак = означает...
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
= — присваивание; == — проверка равенства.
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.
Вызов nextInt(), когда пользователь вводит слово 'hello', вызывает ошибку времени выполнения.
Сложность 1/5 · 100 монет + небольшой бонус за первое выполнение сегодня
Метод ожидает int; текст не совпадает с ним.