-1

The code should output a table for Fahrenheit and Celsius:

public static void main(String[] args) {
    System.out.println("Fahrenheit\tCelsius");
    System.out.println("=======================");
     for(int temp = -45; temp <= 120; temp += 5) //for(int i = 0; i <= 100; i+= 10)
        {
            System.out.printf("%5d       |", temp);
            double sum = (temp + (9.0/5.0)) * 32;   
            System.out.printf("%5d", (int) sum );
            System.out.println();
        }
}  
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
A.B.
  • 7
  • 6

4 Answers4

1

You need to make two changes:

  • Remove cast to int (as it makes the value lose precision)
  • Use ".1f" in printf (as you need to print decimal number and not int)

Below should work:

System.out.printf("%10.1f", sum );
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Wow, nice! Thank you so much. i was trying the .1d and made it more complicated. Is there a way to structure the whole output to look a lot nicer. The output of the Celsius column looks a little strange. – A.B. Apr 02 '17 at 01:28
  • Change `%.1f` to `%10.1f` and you will see the output aligned. I have updated that in the answer. – Darshan Mehta Apr 03 '17 at 08:41
1

How to create a conversion of Fahrenheit to Celsius in java

The most important step IMHO is to understand the problem before ever thinking about coding.

Wikipedia is a good start and searching for Celsius it give us:

[°C] = ([°F] − 32) ×  5⁄9

In Java that would be something like:

celsius = (fahrenheit -32.0) * 5.0 / 9.0;

I think it is best to do that in a separate method so it is easier to test it:

public static double fahrenheitToCelsius(double fahrenheit) {
    double celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
    return celsius;
}

Note 1: it is worth testing this method before going on - two significant temperatures are:

  • 32°F == 0°C (melting point of ice)
  • 212°F == 100°C (boiling point of water)

so just do something quick & dirty like:

System.out.println("32 == " + fahrenheitToCelsius(32));
System.out.println("212 == " + fahrenheitToCelsius(212));

much better, maybe a bit to heavy in that simple case, is to use some framework like JUnit.

Note 2: for creating the table do as posted in the question, but only one printf to take advantage of having the format together at one place (obviously after calling above method):

System.out.printf("%5.0f | %5.1f\n", fahrenheit, celsius);

Note 3: caution with 5/9 - in Java that is interpreted as integer division and would result in zero!

(above code is just a sample and was not tested or debugged

user85421
  • 28,957
  • 10
  • 64
  • 87
0

A better way would be to use the decimal format class

import java.text.DecimalFormat;

int numberToPrint = 10.234;
DecimalFormat threePlaces = new DecimalFormat("##.#");  //formats to 1 decimal place
System.out.println(threePlaces.format(numberToPrint));

Should print:

10.2
cliffdog
  • 29
  • 5
0

If you want to make output result looks like table, you have to use java.util.Formatter.

I rewrote your snippet, a little bit:

public static void main(String[] args) {
    Formatter formatter = new Formatter(System.out);

    formatter.format("%-20s\n",      "---------------------------");
    formatter.format("%-10s %12s\n", "| Fahrenheit |", "Celsius |");
    formatter.format("%-20s\n",      "---------------------------");

    for (int farValue = -45; farValue <= 80; farValue += 5) {
        double celValue = (farValue + (9.0 / 5.0)) * 32;

        formatter.format("| %10d | %10.0f |\n", farValue, celValue);
    }
    formatter.format("%-20s\n", "---------------------------");
}

The output snippet looks exactly like a table:

---------------------------
| Fahrenheit |    Celsius |
---------------------------
|        -45 |      -1382 |
|        -40 |      -1222 |
 ...
|        -30 |       -902 |
---------------------------
catch23
  • 17,519
  • 42
  • 144
  • 217