I'm going to assume you have some sort of special class for these Planets, which you totally should; and that you store each instance of that class in the array named planets
. Therefore, you should create Getters
and Setters
to get the values of each planet, since I don't think your code works as it is. Regardless, I salute you for your determination.
If you want to round the number to 2 decimal places, and round it's result to the nearest ceiling (I don't really know how to express this, see code):
DecimalFormat df = new DecimalFormat("####.##");
df.setRoundingMode(RoundingMode.CEILING);
for (int i = 0; i < planets.length; i++) {
System.out.print(planets[i]);
System.out.println(" " + df.format(planetDiameter[i]));
System.out.println(" " + df.format(planetMass[i]));
System.out.println(" " + df.format(gravity[i]) + "\n");
}
The parameters in the declaration of DecimalFormat, mean the amount of numbers that you want to display; then the rounding mode indicates whether you want to raise the number to the nearest-high value, or lower the number to the nearest-low value.
However, I can't test this since, well, I don't have the planets
array, thus I don't have data to try.
You can try it to see how it works, and I'll give you this, which is the original post from where I got the idea, nice info.
Good luck man.