Program problem: Write a program that generates a two-column table showing Fahrenheit temperatures from -40F to 120F and their equivalent Celsius temperatures. Each line in the table should be 5 degrees F more than the previous one. Both the Fahrenheit and Celsius temperatures should be accurate to 1 decimal place.
My code:
public class Program3_2 {
public static void main(String[] args) {
System.out.println("Fahrenheit\tCelsius");
System.out.println("=======================");
for(int temp = -45; temp <= 120; temp += 5)
{
System.out.printf("%1d ",+temp);
double sum = (5.0/9.0) * (temp - 32);
System.out.printf("%1d", (int)sum );
System.out.println();
}
}
}
Code Issue: The program does display accurate results, but it doesn't seem to output the correct decimal format. I would like the output to display in a straight column for both and show the output in one decimal place. If any one can advise what I did wrong and how to improve, please advise. I am trying to become a better programmer and understand Java. Any advise will help, thank you so much!
Aucun commentaire:
Enregistrer un commentaire