I have a Java program that does a bunch of calculations from some user inputs and at the end it has to print the equation of the plane. Format of the equation of the plane is 6x-2y+3z-4=0.
To get the values 6, -2, 3, & -4 is from a bunch of calculations. So i was thinking to print out the equation is to
System.out.println("Equation is: " + aa + "x" + bb +
"y" + cc + "z" + gg + "=0");
Where aa, bb, cc , gg corresponds to the 4 integers above. But the output is
Equation is: 6x-2y3z-4=0
It seems to print the minus signs in there for the negative numbers but how can i have it print out a plus sign if the number is positive? Like in between -2y3z should be 6x-2y+3z-4=0
%d means number. %0nd means zero-padded number with a length. You build n by subtraction in your example. %s is a string. Your format string ends up being this: "%03d%s", 0, "Apple"
%d: Specifies Decimal integer. %c: Specifies character. %T or %t: Specifies Time and date. %n: Inserts newline character.
By using %n in your format string, you tell Java to use the value returned by System. getProperty("line. separator") , which is the line separator for the current system.
You could use System.format()
:
System.out.format("Equation is: %dx %+dy %+dz %+d = 0\n", aa, bb, cc, gg);
^ ^ ^
Specifying the +
flag would include the sign whether positive or negative.
You can find more information about formatting numeric output here.
You can try using printf()
to display a formatted output:
int aa = 6;
int bb = -2;
int cc = 3;
int gg = -4;
System.out.printf("Equation is: %dx%+dy%+dz%+d=0", aa, bb, cc, gg);
Here you are ussing the format modifier %+d
, to specify that the sign must be displayed, even if the number is positive.
Output:
Equation is: 6x-2y+3z-4=0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With