Converting temperature program
Alright so I'm having trouble creating this program, basically I need to prompt the user to enter a 1 or 2 if 1, you run a convert from Fahrenheit to celsius, or if the user presses 2, the program runs celsius to fahrenheit. Here's what I have so far:
Code :
import java.util.Scanner;
public class TemperatureConverter{
public static void main(String[] args){
Scanner console = new Scanner(System.in);
System.out.println("Please press 1 to convert Fahrenheit to Celsius, or 2 to convert Celsius to Fahrenheit");
int choice = console.nextInt();
if (choice == 1){
ftoc();
}
else if (choice == 2){
ctof();
}
System.out.println("What is the temperature?");
double temperature = console.nextDouble();
System.out.println("Your converted temperature is ");
}
//converts fahrenheit to celsius
public static double ftoc (double degreesF){
double degreesC = 5.0/9.0 * (degreesF - 32);
return degreesC;
}
//converts celsius to fahrenheit
public static double ctof (double degreesC){
double degreesF = 9.0/5.0 * (degreesC + 32);
return degreesF;
}
}
I'm having trouble with what I need to do after the user puts a 1 or a 2 idk how I should go about if the user presses 1 then run this method, but if the user presses 2 run this method. It keeps giving me a bunch of errors saying that it's a variable, but I'm not assigning a variable, I'm calling the method to run that. I'm so confused :confused:
Re: Converting temperature program
your ftoc and ctof methods require you to pass a double as a parameter.
Something like this:
You still need to get a number to convert from the user (use the nextDouble() method in Scanner).
Re: Converting temperature program
Huh? :confused: Do you mean instead of nextInt make it nextDouble? What do I put inside the parenthesis then?
Re: Converting temperature program
try this, just a re-arrange and helloworlds edit.
Code :
import java.util.Scanner;
public class TemperatureConverter{
public static void main(String[] args){
Scanner console = new Scanner(System.in);
System.out.println("Please press 1 to convert Fahrenheit to Celsius, or 2 to convert Celsius to Fahrenheit");
int choice = console.nextInt();
System.out.println("What is the temperature?");
double temperature = console.nextDouble();
double t = 0.0;
if (choice == 1){
t = ftoc(temperature);
}
else if (choice == 2){
t = ctof(temperature);
}
System.out.println("Your converted temperature is " + t );
}
//converts fahrenheit to celsius
public static double ftoc (double degreesF){
double degreesC = 5.0/9.0 * (degreesF - 32);
return degreesC;
}
//converts celsius to fahrenheit
public static double ctof (double degreesC){
double degreesF = 9.0/5.0 * (degreesC + 32);
return degreesF;
}
}