formatting - Why is this Java formatted output not right-justified? -
i'm beginner, , i'm experimenting formatted output.
i'm trying produce table of decimals:
for (int = 0; < 8; i++) { (int j = 0; j < 8; j++) { row = (double) 5*i; column = (double) j + 1; system.out.format("% 6.3f", row/column); system.out.print("\t"); } system.out.format("%n"); }
this produces:
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 5.000 2.500 1.667 1.250 1.000 0.833 0.714 0.625 10.000 5.000 3.333 2.500 2.000 1.667 1.429 1.250 15.000 7.500 5.000 3.750 3.000 2.500 2.143 1.875 20.000 10.000 6.667 5.000 4.000 3.333 2.857 2.500 25.000 12.500 8.333 6.250 5.000 4.167 3.571 3.125 30.000 15.000 10.000 7.500 6.000 5.000 4.286 3.750 35.000 17.500 11.667 8.750 7.000 5.833 5.000 4.375
the api says if '-' flag not thrown, output right-justified, want. have put ' ' after % indicate want output padded spaces when necessary. missing?
(i haven't used homework tag, because not enrolled in course.)
you have added space incorrectly formatting string:
system.out.format("% 6.3f", row/column);
should be:
system.out.format("%6.3f", row/column);
and get:
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 5.000 2.500 1.667 1.250 1.000 0.833 0.714 0.625 10.000 5.000 3.333 2.500 2.000 1.667 1.429 1.250 15.000 7.500 5.000 3.750 3.000 2.500 2.143 1.875 20.000 10.000 6.667 5.000 4.000 3.333 2.857 2.500 25.000 12.500 8.333 6.250 5.000 4.167 3.571 3.125 30.000 15.000 10.000 7.500 6.000 5.000 4.286 3.750 35.000 17.500 11.667 8.750 7.000 5.833 5.000 4.375
Comments
Post a Comment