Doubt about using Printf in Java [duplicated]

0

I am learning how to program, and I have an exercise that asks me the following. Basically I have to print on the screen this text below with printf but I do not even know where to start.

<br> El valor de la variable casado es true </br> <br> El valor de la variable MAXIMO es 999999 </br> <br> El valor de la variable diasem es 1 </br> <br> El valor de la variable diaanual es 300 </br> <br> El valor de la variable miliseg es 1298332800000 </br> <br> El valor de la variable totalfactura es 10350,677734 </br> <br> El valor de la variable totalfactura en notación científica es 1.035068E+04 </br> <br> El valor de la variable poblacion es 6775235741 </br> <br> El valor de la variable sexo es M </br>

I have been looking for information on the Internet about how to use the printf class but my programming level is so low that I do not understand the explanations either and I do not know how to do the exercise. Can someone explain to me what rules I have to follow to use printf to show what the exercise asks me to do on the screen? Thank you very much.

    
asked by Sergio 16.02.2018 в 19:37
source

2 answers

0
System.out.printf("Texto que deseas imprimir");

To print plain text. If you want to pass a variable and print it, I'll give you this example:

double n = 1.25036;
System.out.printf("%.3f %n", n);

Exit:

1,250 
    
answered by 16.02.2018 в 20:27
0

Further breaking down the answer given by @ walter-cordova, the function System.out.printf() is defined as follows System.out.printf(String format, Object...args) , that is, a function that receives as the first parameter the display format for stream of output and secondly the data corresponding to that format.

Practical example:

double x = 1.00001;
char y = 'A';
int z = 13;
String s = "Otro String";

System.out.printf("Esta es una salida con printf donde x: %.3f,%n //%n representa un salto de línea
    y: %c, z: %d",x,y,z);
System.ou.printf("Este es otro String s: %s",s);

Result:

Esta es una salida con printf donde x: 1.000, 
y: A, z: 13
Este es otro String s: Otro String

In summary, instead of concatenating data in a giant String, System.out.printf() allows you to format your output and assign the corresponding values to the selected "tokens" .

Important to remember that there are specific "tokens" for the format of each data type.

Visit this link link for a more technical explanation of how format works and to consult the table of flags and converters or "tokens" allowed.

Here link a similar example of digested that provided by @luis -fernando

    
answered by 20.02.2018 в 02:11