How to get output on screen with java?

Java Help(easy): Why do I get an output of 0?

  • import java.util.Scanner; public class Converter { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num; int fahrenheit; int celsius; System.out.print("\nEnter Fahrenheit temperature: "); fahrenheit = input.nextDouble(); celsius = (5/9)*(fahrenheit-32); System.out.printf("%f Fahrenheit is %f Celsius\n\n", fahrenheit, celsius); } } ====================================== I tried making the variables a double, but that didn't work. No matter what I input, I either get 0 or -0....

  • Answer:

    5 and 9 are integers. Therefore, when you divide 5 / 9, you get 0. 0 * (fahrenheit - 32) = 0. In order to avoid this, you need to use doubles (5.0 / 9.0), not ints (5 / 9): public class Converter { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("\nEnter Fahrenheit temperature: "); double fahrenheit = input.nextDouble(); double celsius = (5.0/9.0)*(fahrenheit-32.0); System.out.printf("%f Fahrenheit is %f Celsius\n\n", fahrenheit, celsius); } }

SafiMoyo at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

You need to declare your variables as double, not int, since your writing the statement "input.nextDouble()" It's gonna take that double and do double calculations. And make it 5.0/9.0 so that it's doing a real division and not an integer division.

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.