I am studying methods and have been given an exercise to do. I am a bit unsure as to what to do with this particular question.
The Question we have been given is:
Modify the above program so the conversion is done in a method.
This is the code I have so far and my problem is when I run the code I get as far as when I enter the letter and it stops.
//Exercise 3 Brian Sheet 5
//Modify the above program so that the conversion is done in a method
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
double temp;
String c = "c";
String f = "f";
String a;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the temperature: ");
temp = input.nextDouble();
input = new Scanner(System.in);
System.out
.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
a = input.nextLine();
if (a.equals(c)) {
celsiusEq(temp);
} else {
Fahren(temp);
}
}
private static double celsiusEq(double celsius) {
double temp;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double fahrenheit) {
double temp;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
} I don't know what I am doing wrong and it is probably something very simple. If anyone could help me, it would be graciously appreciated as I have been looking at this for the past 30 minutes!
returnwithSystem.out.println. The first one allows the caller to use what the called method returns. The second one prints to the screen. Your code calls a method, which returns a double, but does absolutely nothing with the doube value which is returned.