有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我必须创建一个方法(outputDegreesF),它打印下面的信息(温度转换),并且只接受一个参数。如何让变量degreesC分别打印degreesC和fahrenheit,而不只是添加它们?比如“0.032.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) 个答案

  1. # 1 楼答案

    您只需要将数字打印为字符串。 将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更改为所需的数量

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

  2. # 2 楼答案

    我假设您可以创建一个嵌套的Degrees类,并让该类为您执行从摄氏到华氏(或从华氏到摄氏)的转换。下面是一个可运行的示例,用于将摄氏度值数组转换为华氏度:

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

    也可以将其他温度标度应用于上述嵌套类