在Java中,如何让变量分别打印两个双打而不是添加它们?

时间:2018-01-27 03:53:34

标签: java variables double

我必须创建一个方法(outputDegreesF)来打印下面的信息(温度转换)并且只接受一个参数。如何让我的变量 degreesC 分别打印degreesC和fahrenheit而不是仅添加它们?喜欢" 0.0 32.0"。我不能改变任何东西到字符串或Java怪胎。

public class tempChart {

        public static void degreeHeader (String cTitle, String fTitle) {
      System.out.println(cTitle + fTitle);
      }

        public static void outputDegreesF (double degreesC) {
         double fahrenheit = 32.0 + (degreesC * 9.0 / 5.0);
         degreesC = degreesC + fahrenheit;
         System.out.print(degreesC);
        }

      public static void main(String[] args) {
       degreeHeader("Degrees (C)", " Degrees (F)\n");
       System.out.println();
       outputDegreesF(0.0);
       outputDegreesF(5.0);
       outputDegreesF(10.0);
       outputDegreesF(15.0);
       outputDegreesF(20.0);
       outputDegreesF(25.0);
       outputDegreesF(30.0);
       outputDegreesF(35.0);
     }



    }

2 个答案:

答案 0 :(得分:0)

您只需将数字打印为字符串即可。 用

替换outputDegreesF()
public static void outputDegreesF(double degreesC) {
    double fahrenheit = 32.0 + (degreesC * 9.0 / 5.0);

    System.out.print(degreesC + " " + fahrenheit); // Easier
    // Or
    System.out.printf("%.7f %.7f", degreesC, fahrenheit); // Used more and lets you format
}

如果您想更改printf打印的小数位数,请将7更改为您想要的数量。

PS: 您不需要System.out.println();行,因为在上一行中您已经添加了一个新行(字符串中的\n

答案 1 :(得分:0)

我可以设想创建一个嵌套的度数类,并让该类为您进行从摄氏度到华氏度(或华氏度到摄氏度)的转换。这是一个可运行的示例,它将Celsius值的数组转换为Fahrenheit:

package tempchart;

public class TempChart {

    public static void main(String[] args) {
        // The temperatures in Celsius you want to convert
        // to Fahrenheit and table within a 1D double type 
        // array.
        double[] degreesCelsius = 
                {0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0};

        // Declare the Degrees class.
        Degrees deg = new Degrees();
        // Table Header
        System.out.printf("%s | %8s%n", "Celcius", "Fahrenheit");
        System.out.println("========+===========");

        // Generate the table based on the temperatures 
        // held within the degreesCelsius array...
        for (int i = 0; i < degreesCelsius.length; i++) {
            deg.setCelsius(degreesCelsius[i]);
            System.out.printf("%-7.2f | %5.2f%n", deg.celsius, deg.fahrenheit);
        }
    }

    // Static Nested Class - Degrees
    static class Degrees {
        public double celsius;
        public double fahrenheit;

        // Getters And Setters
        public double getCelsius() {
            return celsius;
        }

        public void setCelsius(double celsius) {
            this.celsius = celsius;
            //Convert to Fahrenheit - Multiply by 9, then divide by 5, then add 32
            this.fahrenheit = (((celsius * 9) / 5) + 32);
        }

        public double getFahrenheit() {
            return fahrenheit;
        }

        public void setFahrenheit(double fahrenheit) {
            this.fahrenheit = fahrenheit;
            //Convert to Celcius - Deduct 32, then multiply by 5, then divide by 9
            this.celsius = (((fahrenheit - 32.0) * 5) / 9);
        }
    }
}

您还可以将其他温度刻度应用于上述嵌套类。