25th Oct 2019, 2:40 PM
Sanzid Sadman
Sanzid Sadman - avatar
10 Respuestas
+ 11
Sanzid Sadman The Java printf() method is used to write the formatted strings. The 'f' in 'printf' keyword means 'formatted'. For example: int x = 15; float y = 5.6f; char ch = 'S'; String str = "Hi there!"; // Display results: System.out.printf( "The formatted string: %d %f %c %s", x, y, ch, str); • The list of conversion characters that you may use in the 'printf': %d – for signed decimal intege %f – for the floating point %o – octal number %c – for a character %s – a string %i – use for integer base 10 %u – for unsigned decimal number %x – hexadecimal number %% – for writing % (percentage) %n – for new line = \n You may use the precision that specifies the number of digits after the decimal point for floating numbers. For example: double x = 56.1525; double y = 26.2481119; System.out.printf( "x = %.2f %n y = %.4f", x, y ); Output: x = 56.15 y = 26.2481
25th Oct 2019, 6:40 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 10
Sanzid Sadman Here is an useful link, • A 'printf' format reference page ( cheat sheet ) — https://alvinalexander.com/programming/printf-format-cheat-sheet
25th Oct 2019, 5:29 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 7
David Carroll 👍 You gave a great answer and I doubt there is anything to add.🍻
25th Oct 2019, 5:19 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
I recommend you to give <distance> variable (declared at line 21) another name, just to evade from confusion. Because 'distance' is also a method name in there. You could also consult with the original code author.
25th Oct 2019, 3:05 PM
Ipang
+ 6
The compiler is able to differentiate between the local variable of type double on the left side of the equal operator and the static method sharing the same name on the right side. When the code is compiled, the named symbols will resolve to different memory addresses and byte code commands. Danijel Ivanović Do you want to add any insight? -------- Update: ---- Sanzid Sadman DOH! 🤦‍♂️I just realized the question was asking about the code starting from line 35 and not the line itself. Still yet, I thought he referenced the wrong line believing 34 was the line in question as it looks so strange and would not work in dynamically typed languages. Sorry for posting an irrelevant answer. Hopefully, it will be of value to others. 🤷‍♂️😉
25th Oct 2019, 3:13 PM
David Carroll
David Carroll - avatar
+ 6
Yes, David Carroll me too...😄 Sanzid Sadman Line 35? System.out.println();
25th Oct 2019, 5:24 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 5
%d is for integers and %f is for decimal numbers, %.2f means number will get Rounded off to 2 decimal places(you can see more about them in C lesson here). if you are asking for variable name same as function name then it is allowed in java, you can read more about it here https://stackoverflow.com/questions/9960560/java-instance-variable-and-method-having-same-name
25th Oct 2019, 4:38 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
%d displays the integer and %f displays a float System.out.printf( " The distance between points:\n A(%d, %d) and B(%d, %d) \n\n distance = %.2f", x1, y1, x2, y2, distance); The first %d displays x1, the second %d displays y1, third %d displays x2, fourth %d displays y2, and %.2f displays distance with 2 decimal places
25th Oct 2019, 3:13 PM
Odyel
Odyel - avatar
+ 3
it displays the results
25th Oct 2019, 3:07 PM
Airree
Airree - avatar
- 1
Hello, Does anyone know how to make a mitisse connection with Java?
27th Oct 2019, 3:50 AM
Jorge Misael
Jorge Misael - avatar