Accessing a variable in different methods Java -
i studying methods , have been given exercise do. bit unsure particular question.
the question have been given is:
modify above program conversion done in method.
this code have far , problem when run code far when enter letter , stops.
//exercise 3 brian sheet 5
//modify above program conversion done in 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 temperature: "); temp = input.nextdouble(); input = new scanner(system.in); system.out .println("please enter whether wish convert celsius or fahrenheit(c or f)"); = 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; }
} don't know doing wrong , simple. if me, graciously appreciated have been looking @ past 30 minutes!
here need interchange temp , celsius varables work
private static double celsiuseq(double celsius) { double temp; //here problem celsius = (temp - 32) * 5 / 9; return celsius; }
here need interchange temp , fahrenheit varables work
private static double fahren(double fahrenheit) { double temp; //here problem fahrenheit = temp * 9 / 5 + 32; return fahrenheit; }
correction here
private static double celsiuseq(double temp){ double celsius; celsius = (temp - 32) * 5 / 9; return celsius; } private static double fahren(double temp){ double fahrenheit; fahrenheit = temp * 9 / 5 + 32; return fahrenheit; }
update request
returntype functionname(argumenttype argument2 ,argumenttype argument2,....argumenttype argumentn ){ // local variable declaration variableype variable1; variableype variable2; ---------------------- variableype variablen; calculation return value based on return type; }
see more details here
http://www.tutorialspoint.com/java/java_variable_types.htm
http://docs.oracle.com/javase/tutorial/java/javaoo/variables.html
http://docs.oracle.com/javase/tutorial/java/javaoo/arguments.html
Comments
Post a Comment