Assignment and Input · Affectation et Entrée
| English | Français |
|---|---|
| assignment statement/əˈsaɪnmənt ˈsteɪtmənt/ | instruction d'affectation |
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.
L'instruction d'affectation
- Une instruction d'affectation 赋值语句 place une valeur dans une variable :
x = 5; - Le simple
=signifie « stocker dans », pas « est égal à ». - Java évalue d'abord le côté droit, puis stocke le résultat dans la variable de gauche.
count = count + 1;lit l'anciencount, ajoute1, et le stocke à nouveau.
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;
Flux de droite à gauche
- Pensez à
=comme une flèche pointant vers la gauche :total ← price * qty. - La variable de gauche doit déjà être déclarée (avec un type).
- Une affectation ultérieure écrase l'ancienne valeur — la boîte ne contient que la plus récente.
- Vous pouvez déclarer et assigner en une seule ligne :
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();
Lecture d'entrées avec Scanner
- Pour lire l'entrée utilisateur, Java utilise un objet
Scannerlié àSystem.in. Scanner sc = new Scanner(System.in);le crée.sc.nextInt()lit unint;sc.nextDouble()undouble;sc.next()un mot.- Stockez ce que vous lisez dans une 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.
Associer la lecture au type
- Utilisez la méthode de lecture qui correspond au type attendu.
nextInt()pour les nombres entiers,nextDouble()pour les décimaux,nextLine()pour une ligne entière.- Une incompatibilité (ex.
nextInt()sur le texte "hello") provoque une erreur d'exécution. - Lisez dans une variable correctement typée afin que le reste du code fonctionne.
= 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.
= est une affectation, pas une égalité — et cela se lit de droite à gauche. x = x + 1 n'est pas une contradiction ; il calcule x + 1 et le stocke à nouveau dans x. (Tester l'égalité plus tard utilise ==, un opérateur différent.) Avec Scanner, associez la méthode au type : nextInt() lit un entier, et lire un non-nombre lance une erreur d'exécution.
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.
Lecture et mise à jour d'un score :
int score = sc.nextInt();→scorereçoit le nombre tapé par l'utilisateur.score = score + 10;→ le côté droitscore + 10est calculé, puis stocké à nouveau.- Si l'utilisateur a tapé
40,scorevaut maintenant50.
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.
Une instruction d'affectation x = value; évalue d'abord le côté droit, puis le stocke dans la variable de gauche (= signifie "stocker dans", pas égalité). Lisez l'entrée avec un Scanner : nextInt(), nextDouble() ou next()/nextLine() — associez la méthode au type attendu.
Right side first, then store · Côté droit d'abord, puis stocker
Each assignment evaluates the right side, then overwrites the box. · Chaque affectation évalue le côté droit, puis écrase la variable.
In Java, the single = sign means... · En Java, le signe = simple signifie...
= is assignment; == tests equality. · = est l'affectation ; == teste l'égalité.
score is 40, then score = score + 10; runs. What is score now? · score vaut 40, puis score = score + 10 ; s'exécute. Quelle est maintenant la valeur de score ?
Right side 40+10 = 50 is stored back into score. · Le côté droit 40+10 = 50 est stocké à nouveau dans score.
Which Scanner method reads a whole number? · Quelle méthode Scanner lit un nombre entier ?
nextInt() reads an int; nextDouble() reads a decimal. · nextInt() lit un int ; nextDouble() lit un décimal.
Java evaluates the right side of an assignment before storing into the left variable. · Java évalue le côté droit d'une affectation avant de stocker dans la variable de gauche.
Right side first, then store — that's how x = x + 1 works. · Côté droit d'abord, puis stocker — c'est ainsi que x = x + 1 fonctionne.
Calling nextInt() when the user types the word 'hello' causes a run-time error. · Appeler nextInt() lorsque l'utilisateur tape le mot 'hello' provoque une erreur d'exécution.
The method expects an int; text mismatches it. · La méthode attend un int ; le texte ne correspond pas.