Reputation: 161
I am a beginner programmer
I have an assignment which is Write a program that converts a Fahrenheit degree to Celsius using formula: Celsius = (5/9)(Fahrenheit - 32)
The problem is I am always getting the same value -17.78 whatever I give value in input.
Here down there is my code !!!
package com.temperatureconversion;
import java.util.Scanner;
public class TemperatureConversion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double F = 0.0; // Temperature in Fahrenheit
double C = 0.0; // Temperature in celsius
C = 5.0 /9 * (F - 32);
System.out.print("Enter temperature in fahrenheit: ");
F = input.nextDouble();
System.out.printf("The celsius value of %10.2f is %2.2f", F, C);
}
}
What's wrong with the above code ?
Upvotes: 0
Views: 821
Reputation: 8657
Your F
value is always the same 0.0
, because you're asking for its value after your calculations, so you need to move the getting of F
value before doing calculation.
double F = 0.0; // Temperature in Fahrenheit
double C = 0.0; // Temperature in celsius
//ASK for value.
System.out.print("Enter temperature in fahrenheit: ");
F = input.nextDouble();
// Do your calculations.
C = 5.0 /9 * (F - 32);
Upvotes: 2
Reputation: 1874
Try this , ask first and then calculate
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double F = 0.0; // Temperature in Fahrenheit
double C = 0.0; // Temperature in celsius
System.out.print("Enter temperature in fahrenheit: ");
F = input.nextDouble();
C = 5.0 /9 * (F - 32);
System.out.printf("The celsius value of %10.2f is %2.2f", F, C);
}
Upvotes: 0