如何从方法返回多个双打

时间:2018-11-03 06:02:20

标签: java methods

我认为for循环会在方法中循环并返回双精度数,直到结束为止,但它只执行一次该过程,并一遍又一遍地输出一个数字,直到完成为止

public class CtoFTableDisplay {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String string1 = "°C";
        String string2 = "°F";

        double Celsius2 = -100;
        double fahrenheit = 0;
        fahrenheit = CtoF(Celsius2);
        //double fahrenheit = CtoF(Celsius2);
        System.out.format("%5s%9s", string1, string2);

        for (Celsius2 = -100; Celsius2 <= 100; Celsius2++) {
            System.out.format ("\n%3.1f%9.1f", Celsius2, fahrenheit);

            System.out.println();
        }

    }

    public static double CtoF(double Celsius) {

            double [] ans = new double [200];
            double fahrenheit = 0;

            for (Celsius = -100; Celsius <= 100; Celsius++) {
                fahrenheit = (Celsius * 9/5) + 32;
            }
           return fahrenheit;
    }
}

1 个答案:

答案 0 :(得分:0)

将循环更改为:

  for (Celsius2 = -100; Celsius2 <= 100; Celsius2++) {
        System.out.format ("\n%3.1f%9.1f", Celsius2, fahrenheit);
        fahrenheit = CtoF(Celsius2);
        System.out.println();
    }