Bonjour, Je ne comprends pas comment la sortie peut se faire 12 puis 2 car la sortie écran ne change pas x = 2 et y = 12. J'ai bien décommenté une ligne mais pour autant je n'ai pas compris la finalité. Quelqu'un peut m'expliquer ? Merci d'avance
Sortie 12 puis 2
Discussion en cours
Tu dois être connecté(e) pour laisser un commentaire
28 décembre 2022, 15:01
Simply stated: Java executes code line by line in order. It is a lot more complicated than that but the full set of rules are sure to confuse you at this point so this is sufficient knowledge to complete this task. In this code, starting at the line:You can start reading the code as follows: step 1: an int variable called 'x' is declared and set to a value of 2 step 2: an int variable called 'y' is declared and set to a value of 12 The next 2 lines of code are commented out and the compiler skips them completely. step 3: the next line of code is: With the equals operator (=), the operand on the left is going to be set to the operand on the left. In this line of code 'x' is on the left and 'y - x' is on the left. The compiler will first replace any operand variables on the right with their values, so this line of code will look like this: This evaluates to 10 and the variable 'x' is then set to the value of 10. step 4: The next line of code is considered: as stated above, the compiler replaces variables first with their values. The variable 'y' has not changed and remains at 12, but the variable 'x' has been changed at this point in code from its initial value of 2 to the value of 10. So the expression first is changed to: So 12 minus 10 is equal to 2, which 'y' will be now set to. step 5: next line of code: 'x', value 10, is output to screen step 6: and finally 'y', value 2, is output to screen
28 décembre 2022, 15:08
The above explanation is for the code as it is given before you start making changes. Using what I described above try figuring out what will happen when you uncomment one line, then figure out what will happen when you uncomment the other one instead. It is not hard to figure out, though when you are new to programming it doesn't come intuitively. Try getting some scrap paper, or a notebook, and write down each step to the end of the code to see if you can get the correct output before running the code.
30 décembre 2022, 17:40
Thank you very much. Your explanation is really clear. In my day life I use more "words" than "numbers" but I will try to read more carefully and don't try to rush. Thanks again and have a good evening.
30 décembre 2022, 18:14
Just keep doing your best. Programming is really hard when you first start learning it (search google about this and you can find thousands of articles), but it starts making sense after time. Anyone that knows how to program now spent plenty of time being frustrated when they were first learning too.