我正在學習方法並且已經做了一個練習。對於如何處理這個特定的問題,我有點不確定。用不同的方法訪問一個變量Java
我們已獲得的問題是:
Modify the above program so the conversion is done in a method.
這是我到目前爲止的代碼,我的問題是,當我運行代碼,我走這麼遠,當我輸入字母的和停止。
//Exercise 3 Brian Sheet 5
//從而使轉化在方法
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;
}
} 我不知道我做錯了做,它可能是一件很簡單的修改上面的程序。如果任何人都可以幫助我,我會在過去的30分鐘內一直關注這件事情。
我希望這可以幫助你:http://www.java-made-easy.com/variable-scope.html –
你混淆''return'with System.out.println'。第一個允許調用者使用被調用方法返回的內容。第二個打印到屏幕上。你的代碼調用一個方法,該方法返回一個double,但與返回的doube值完全沒有關係。 –