Assignment and Input · Atribuição e Entrada
| English | Português |
|---|---|
| assignment statement/əˈsaɪnmənt ˈsteɪtmənt/ | instrução de atribuição |
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.
A instrução de atribuição
- Uma instrução de atribuição 赋值语句 coloca um valor em uma variável:
x = 5; - O único
=significa "armazenar em", não "é igual a". - Java avalia o lado direito primeiro, depois armazena o resultado na variável do lado esquerdo.
count = count + 1;lê o antigocount, adiciona1e armazena de volta.
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;
Fluxo da direita para a esquerda
- Pense em
=como uma seta apontando para a esquerda:total ← price * qty. - A variável à esquerda já deve estar declarada (com um tipo).
- Uma atribuição posterior sobrescreve o valor antigo — a caixa contém apenas o mais recente.
- Você pode declarar e atribuir em uma única linha:
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();
Lendo entrada com Scanner
- Para ler a entrada do usuário, Java usa um objeto
Scannervinculado aSystem.in. Scanner sc = new Scanner(System.in);o cria.sc.nextInt()lê umint;sc.nextDouble()umdouble;sc.next()uma palavra.- Armazene o que você leu em uma variável:
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.
Combine a leitura com o tipo
- Use o método de leitura que corresponde ao tipo que você espera.
nextInt()para números inteiros,nextDouble()para decimais,nextLine()para uma linha inteira.- Um desajuste (ex.
nextInt()no texto "hello") causa um erro de tempo de execução. - Leia em uma variável com o tipo correto para que o resto do código funcione.
= 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.
= é atribuição, não igualdade — e flui da direita para a esquerda. x = x + 1 não é uma contradição; ele calcula x + 1 e armazena de volta em x. (Testar igualdade depois usa ==, um operador diferente.) Com Scanner, combine o método com o tipo: nextInt() lê um inteiro, e ler um não-número lança um erro de tempo de execução.
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.
Lendo e atualizando uma pontuação:
int score = sc.nextInt();→scorerecebe qualquer número que o usuário digitou.score = score + 10;→ lado direitoscore + 10é calculado, então armazenado de volta.- Se o usuário digitou
40,scoreagora é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.
Uma instrução de atribuição x = value; avalia primeiro o lado direito, depois armazena na variável da esquerda (= é "armazenar dentro", não igualdade). Leia entrada com um Scanner: nextInt(), nextDouble() ou next()/nextLine() — combine o método com o tipo que você espera.
Right side first, then store · Lado direito primeiro, depois armazena
Each assignment evaluates the right side, then overwrites the box. · Cada atribuição avalia o lado direito, depois sobrescreve a caixa.
In Java, the single = sign means... · Em Java, o sinal = único significa...
= is assignment; == tests equality. · = é atribuição; == testa igualdade.
score is 40, then score = score + 10; runs. What is score now? · score vale 40, então score = score + 10; executa. Qual é o score agora?
Right side 40+10 = 50 is stored back into score. · Lado direito 40+10 = 50 é armazenado novamente em score.
Which Scanner method reads a whole number? · Qual método Scanner lê um número inteiro?
nextInt() reads an int; nextDouble() reads a decimal. · nextInt() lê um int; nextDouble() lê um decimal.
Java evaluates the right side of an assignment before storing into the left variable. · Java avalia o lado direito de uma atribuição antes de armazenar na variável esquerda.
Right side first, then store — that's how x = x + 1 works. · Lado direito primeiro, depois armazena — assim funciona x = x + 1.
Calling nextInt() when the user types the word 'hello' causes a run-time error. · Chamar nextInt() quando o usuário digita a palavra 'hello' causa um erro de tempo de execução.
The method expects an int; text mismatches it. · O método espera um int; texto não corresponde a ele.