Assignment and Input · Asignación y entrada
| English | Español |
|---|---|
| assignment statement/əˈsaɪnmənt ˈsteɪtmənt/ | declaración de asignación |
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.
La sentencia de asignación
- Una sentencia de asignación puts a value into a variable:
x = 5; - El único
=significa "almacenar en," no "es igual a." - Java evalúa primero el lado derecho, luego almacena el resultado en la variable del lado izquierdo.
count = count + 1;lee el valor antiguo decount, le suma1y lo vuelve a almacenar.
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;
Flujo de derecha a izquierda
- Piensa en
=como una flecha apuntando hacia la izquierda:total ← price * qty. - La variable del lado izquierdo ya debe estar declarada (con un tipo).
- Una asignación posterior sobrescribe el valor antiguo — la caja solo contiene el más reciente.
- Puedes declarar y asignar en una sola línea:
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();
Lectura de entrada con Scanner
- Para leer la entrada del usuario, Java usa un objeto
Scannervinculado aSystem.in. Scanner sc = new Scanner(System.in);lo crea.sc.nextInt()lee unint;sc.nextDouble()undouble;sc.next()una sola palabra.- Almacena lo que leas en una 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.
Coincide la lectura con el tipo
- Usa el método de lectura que coincida con el tipo que esperas.
nextInt()para números enteros,nextDouble()para decimales,nextLine()para una línea completa.- Una coincidencia incorrecta (por ejemplo, usar
nextInt()con el texto "hello") provoca un error de ejecución. - Lee dentro de una variable con el tipo correcto para que el resto del 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.
= es asignación, no igualdad — y fluye de derecha a izquierda. x = x + 1 no es una contradicción; calcula x + 1 y lo vuelve a almacenar en x. (Para probar la igualdad después se usa ==, un operador diferente.) Con Scanner, empareja el método con el tipo: nextInt() lee un entero, y leer un valor no numérico lanza un error de ejecución.
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.
Leer y actualizar una puntuación:
int score = sc.nextInt();→scorerecibe cualquier número que haya escrito el usuario.score = score + 10;→ se calcula el lado derechoscore + 10, luego se almacena nuevamente.- Si el usuario escribió
40,scoreahora es50.
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.
Una sentencia de asignación x = value; evalúa primero el lado derecho, luego lo almacena en la variable del lado izquierdo (= es "almacenar en", no igualdad). Lee la entrada con un Scanner: nextInt(), nextDouble(), o next()/nextLine() — empareja el método con el tipo que esperas.
Right side first, then store · Lado derecho primero, luego almacenar
Each assignment evaluates the right side, then overwrites the box. · Cada asignación evalúa el lado derecho, luego sobrescribe la caja.
In Java, the single = sign means... · En Java, el signo = único significa...
= is assignment; == tests equality. · = es asignación; == prueba igualdad.
score is 40, then score = score + 10; runs. What is score now? · score es 40, entonces score = score + 10; se ejecuta. ¿Cuál es el valor actual de score?
Right side 40+10 = 50 is stored back into score. · El lado derecho 40+10 = 50 se almacena nuevamente en score.
Which Scanner method reads a whole number? · ¿Qué método Scanner lee un número entero?
nextInt() reads an int; nextDouble() reads a decimal. · nextInt() lee un int; nextDouble() lee un decimal.
Java evaluates the right side of an assignment before storing into the left variable. · Java evalúa el lado derecho de una asignación antes de almacenar en la variable izquierda.
Right side first, then store — that's how x = x + 1 works. · Lado derecho primero, luego almacenar — así funciona x = x + 1.
Calling nextInt() when the user types the word 'hello' causes a run-time error. · Llamar a nextInt() cuando el usuario escribe la palabra 'hello' causa un error en tiempo de ejecución.
The method expects an int; text mismatches it. · El método espera un int; el texto no coincide con él.