在Main中调用方法

时间:2017-09-21 00:21:04

标签: java methods

我试图从主要方法之外调用一个方法,而我却不知道如何去做。我可能会以错误的方式解决这个问题。例如,我希望用户输入华氏温度,然后调用fahrenheitToCelsius方法进行转换并返回转换后的温度。

这是我到目前为止所做的:

import java.util.Scanner;

public class Converter {

// conversions
private static final double GRAMS_PER_OUNCE = 28.3495;
private static final double CENTIMETERS_PER_FEET = 30.48;

public static double fahrenheitToCelsius (double f) {

    return (f - 32) / 1.8;
}

public static double feetToCentimeters (double feet) {

    return feet * CENTIMETERS_PER_FEET;
}

public static double ouncesToGrams (double ounces) {

    return ounces * GRAMS_PER_OUNCE;
}

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.println("Enter a number in Fahrenheit: ");
    System.out.print(in.nextDouble());

}
}

1 个答案:

答案 0 :(得分:1)

public static void main(String[] args) {

   Scanner in = new Scanner(System.in);

   System.out.println("Enter a number in Fahrenheit: ");
   double far = in.nextDouble();
   double cel = fahrenheitToCelsius (far); 
   System.out.println (cel); 
}

喜欢这个?