I'm learning to program, I'm doing exercises, this code is supposed to have errors but I don't see the errors anywhere.
11. System.out.println("El valor de x es ", x, " y el valor de y es ", y);
12. System.out.println("El resultado de x + y es " + (x + y));
13. System.out.println("El resultado de x - y es " + (x - y));
14. System.out.printf("\n%s%s\n","División entera:","x / y = ",(x/y));
15. System.out.println("Resto de la división entera: x % y = " + (x % y));
16. System.out.printf("El valor de f1 es %f y el de f2 es %f\n",f1,f2);
17. System.out.println("El resultado de f1 / f2 es " + (f1 / f2))
Let's see, I correct the code pointing out the errors and you tell me:
11. System.out.println("El valor de x es x y el valor de y es y");
12. System.out.println("El resultado de x + y es " + (x + y));
13. System.out.println("El resultado de x - y es " + (x - y));
14. System.out.printf("\n%s%s\n","División entera:","x / y = ",(x/y));
15. System.out.println("Resto de la división entera: x % y = " + (x % y));
16. System.out.printf("El valor de f1 es %f y el de f2 es %f\n",f1,f2);
17. System.out.println("El resultado de f1 / f2 es " + (f1 / f2))
Let's see, I really don't understand this code. I put my doubts:
· I understand that in the content of what you print on the screen with System.out.println you can't put quotes inside because you already have everything in quotes, right?
· In the following lines of code when it puts quotes it tells you: "the result of x + y is " the truth is that I don't understand what those quotes mean.
· On line 14 I don't understand the code (14. System.out.printf("\n%s%s\n","División entera:","x / y = ",(x/y));)
. I know that printf is used for formatting in Java. So: it gives me two line breaks and in between an s% that I don't know what it's for, then it gives me integer division which I understand is text that appears on the screen and then x/y repeats it twice.
· The rest of the lines I don't understand at all either, I mean... can someone explain to me what rules there are for putting quotes, what these operators are for, what syntax rules they have, etc.?
Thank you very much, greetings.
Definitions
System.out.println(String x)
will always accept a single parameter, be it of data type boolean, char, char[], double, float, int, long, Object or String.printf(String format, Object... args
) will have to have as its first parameter the text and format that the parameters are going to have and then as many arguments as we want and have defined in the text format, separated by commas.In case there are more parameters than format specifiers, these parameters will be ignored.
Quotes and System.out.println()
Quotes can be used inside the function
System.out.println()
, since you are going to print a text on the screen and everything that is contained within quotes will be considered as text.In case you want to concatenate a text with the result of a variable, you will have to do it through the symbol
+
and not with commas (as in your example 11).For example, imagining that we have
x=5
:It would print the following:
That is, it is not that you repeat the x twice, but that what is inside the quotes evaluates it "as it is", that is, it does not interpret its value. Instead, when we use the variable
x
outside the quotes it will show us the corresponding value of that variable.The same happens in your examples 12, 13, 15 and 17, only that in these cases it shows on the screen the result of operations instead of only the result of a variable.
NOTE: You will have to be careful in case you want to show a String in which quotes are shown, since these are special characters and you will have to escape them. To escape a special character and display it "as is" in a String, you must use the backslash
\
just before the special character.An example would be:
And the result would be:
printf
It is used to print a text with a certain format on the screen.
It has some "formatters" for the output variables. The ones I see you use are:
That is, taking your example 16:
What it's going to do is in the first it's
%f
going to print the value off1
as a float and in the second it's%f
going to printf2
as a float.In this case, when you print the result it will not show you the content of the quotes "as it is" as it used to happen with
System.out.println
, but instead it will replace each one of them%f
with the result of the variables that you pass as parameters in order of location (the first%f
with the first parameter, the second%f
with the second parameter and so on...)