2013-10-07 37 views
0

我正在學習方法並且已經做了一個練習。對於如何處理這個特定的問題,我有點不確定。用不同的方法訪問一個變量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分鐘內一直關注這件事情。

+0

我希望這可以幫助你:http://www.java-made-easy.com/variable-scope.html –

+3

你混淆''return'with System.out.println'。第一個允許調用者使用被調用方法返回的內容。第二個打印到屏幕上。你的代碼調用一個方法,該方法返回一個double,但與返回的doube值完全沒有關係。 –

回答

2

在這裏,你需要互換溫度和攝氏varables到這裏正常工作

private static double celsiusEq(double celsius) { 
     double temp; //here is the problem 
     celsius = (temp - 32) * 5/9; 
     return celsius; 

    } 

需要互換溫度和華氏varables才能正常工作

private static double Fahren(double fahrenheit) { 
     double temp; //here is the problem 
     fahrenheit = temp * 9/5 + 32; 
     return fahrenheit; 
    } 

修正這裏

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; 
} 

更新請

returntype functionname(argumenttype argument2 ,argumenttype argument2,....argumenttype argumentN ){ 
// local variable declaration 
variableype variable1; 
variableype variable2; 
---------------------- 
variableype variableN; 
Your calculation 

    return your value based on the return type; 

} 

這裏

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

+0

OMG YES !!!謝謝! – user2830571

+0

那麼要在一個方法中使用一個變量,它需要在括號中聲明? – user2830571

+1

這是一個說法。它是一個在調用方法時傳入的值。 –

0

看到更多的細節與此下面的代碼

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)){ 
     System.out.println(celsiusEq(temp)); 
    }else{ 
     Fahren(temp); 
    } 


    } 


    private static double celsiusEq(double celsius){ 
    double temp = 0; 
    celsius = (temp - 32) * 5/9; 
    return celsius; 

} 

    private static double Fahren(double fahrenheit){ 
    double temp = 0; 
    fahrenheit = temp * 9/5 + 32; 
    return fahrenheit; 
} 

} 
嘗試

輸出中

Please enter the temperature: 
250 
Please enter whether you wish to convert to Celsius or Fahrenheit(c or f) 
f 
32.0 
1

它很簡單,其實。下面的代碼:

攝氏華氏

private static double celsiusToFahrenheit(double celsius) 
{ 
    double fahrenheit; 
    fahrenheit = ((celsius * 9)/5) + 32; 
    return fahrenheit; 
} 

華氏溫度爲攝氏

private static double fahrenheitToCelsius(double fahrenheit) 
{ 
    double celsius; 
    celsius = ((fahrenheit - 32) * 5)/9; 
    return celsius; 
} 

,同時執行多個操作始終使用括號。它是一個很好的編程習慣。

這裏是爲你的代碼錯誤:

private static double celsiusEq(double celsius) 
{ 
    double temp; //TEMP HAS NO VALUE 
    celsius = (temp - 32) * 5/9; //STILL YOU ARE USING IT 
    return celsius;  
} 

private static double Fahren(double fahrenheit) 
{ 
    double temp; //TEMP HAS NO VALUE 
    fahrenheit = temp * 9/5 + 32; //STILL YOU ARE USING IT 
    return fahrenheit; 
} 

使用攝氏和華氏變量相反的,你使用臨時的。

1

這是你的代碼段:

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; 
} 

幾個更正需要做:

1)既然你是路過temp作爲參數,爲什麼不您只需使用,在你的函數這樣的:

private static double Fahren(double temp){ 
    double fahrenheit; 
    fahrenheit = temp * 9/5 + 32; 
    return fahrenheit; 
} 

2.)嘗試使這樣的方法public而不是private。提供更容易的訪問和操作。

3)由於你的函數返回一個double,則需要採集結果以打印/修改。就像這樣:

double answerInCelcius = celsiusEq(temp); 
System.out.println("Answer in Celsius is :" +answerInCelsius); 

希望它能幫助:)

1

你需要改變你的方法

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; 
} 

你的變量名得到了互換。

0

我會做這樣的事情:

import java.util.Scanner; 
public class Exercise3 { 

    public static void main(String[] args) { 
     double temp; 
     String a; 

     System.out.println("Please enter the temperature: "); 
     Scanner input = new Scanner(System.in); 
     temp = input.nextDouble(); 


     System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)"); 
     a = input.nextLine(); 
     switch(a) 
     { 
      case "c": 
       temp = toCelsius(temp); 
       System.out.println("Temp in Celsius: " + temp); 
       break; 
      case "f": 
       temp = toFahren(temp); 
       System.out.println("Temp in Fahren: " + temp); 
       break; 
      default: 
       System.out.println("Invalid Entry"); 
     } 

    } 


    private static double toCelsius (double fahren){ 
     return (fahren - 32) * 5/9; 
    } 

    private static double toFahren(double celsius){ 
     return celsius * 9/5 + 32; 
    } 

} 

但是,嘿!許多其他人打我。

0
private static double celsiusEq(double temp) { 
    return (temp - 32) * 5/9; 

} 

private static double Fahren(double temp) { 
    return temp * 9/5 + 32; 
} 

不要忘記打印結果vlaue像System.out.println(celsiusEq(temp)); :-)

0

你只是混淆副作用方法中的溫度和攝氏度變量。所以我改變了代碼,以下是你的代碼的更新版本。

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 temp) { 
    double celsius; 
    celsius = (temp - 32) * 5/9; 
      System.out.println("AFTER CONVERTION " + celsius); 
    return celsius; 

} 

private static double Fahren(double temp) { 
    double fahrenheit; 
    fahrenheit = temp * 9/5 + 32; 
      System.out.println("AFTER CONVERTION " + fahrenheit); 
    return fahrenheit; 
} 
    } 
相關問題