用温度格式化代码

时间:2017-05-30 06:23:40

标签: java

处理一个问题,得到了我需要的华氏度列表,但我真的不知道如何在它旁边添加另一个列表并让它做数学运算。请帮忙,谢谢。

问题:

生成两列表的程序,显示-40°F至120°F的华氏温度及其相应的摄氏温度。表中的每一行应比前一行多5华氏度。华氏温度和摄氏温度都应精确到小数点后1位。

我的尝试:

    package chapter5;
    public class Assignment2 {
        public static void main(String[] args) {
        //T(°C) = (T(°F) - 32) × 5/9
        int F = 120;
        int count = 0;
        int C = (F - 32) * (5/9);

        while (F >= -40 && F <= 120 ){
            if (F % 5 == 0){
                System.out.printf("%-5d",F);
                count++;    
            }
            if (count == 1){
               System.out.println();
               count = 0;
            }
            F--;
        }
    }   
}

5 个答案:

答案 0 :(得分:1)

/* package whatever; // don't place package name! */
package chapter5;

/* Name of the class has to be "Main" only if the class is public. */

public class Assignment2 {
    public static void main(String[] args) {
        //T(°C) = (T(°F) - 32) × 5/9
        final String DEGREE  = "\u00b0";
        int F = -40;        
        float C = 0.0F;

        while (F <= 120 ){

            C = (F - 32)*(0.5556F);

            System.out.printf("%d%sF  \t\t\t%.1f%sC\n", F, DEGREE, C, DEGREE);
            F = F + 5;
        }
    }   
}

<强>输出

-40°F           -40.0°C
-35°F           -37.2°C
-30°F           -34.4°C
-25°F           -31.7°C
-20°F           -28.9°C
-15°F           -26.1°C
-10°F           -23.3°C
-5°F            -20.6°C
0°F             -17.8°C
5°F             -15.0°C
10°F            -12.2°C
15°F            -9.4°C
20°F            -6.7°C
25°F            -3.9°C
30°F            -1.1°C
35°F            1.7°C
40°F            4.4°C
45°F            7.2°C
50°F            10.0°C
55°F            12.8°C
60°F            15.6°C
65°F            18.3°C
70°F            21.1°C
75°F            23.9°C
80°F            26.7°C
85°F            29.4°C
90°F            32.2°C
95°F            35.0°C
100°F           37.8°C
105°F           40.6°C
110°F           43.3°C
115°F           46.1°C
120°F           48.9°C

答案 1 :(得分:0)

查看printf format specification,以标签模式打印出F和C温度。

此外,您需要在浮点数或双精度计算中进行计算。

你可以用5的增量编写循环,而不是循环遍历所有内容并检查%5。

答案 2 :(得分:0)

我使用System.out.printf格式化eclipse控制台中的输出。

public static void main(String[] args) {
        double F = -40;

        for (; F < 120; F += 5) {
            double C = (F - 32) * 5 / 9;
            C = BigDecimal.valueOf(C).setScale(1,RoundingMode.CEILING).doubleValue();
            System.out.printf("%-10s %-10s \n", F, C);
        }
    }

此程序将一个Farenheit值转换为celsium,在两列中打印并将farenheit值增加5并再次将其转换为celsium。

它使用java.lang.BigDecimal将double值舍入到1位小数。这是在方法setScale(1,RoundingMode.CEILING)

中完成的

答案 3 :(得分:0)

我只有一个for循环,我减少了5,所以我不必检查它是否可以除以5。 在我的解决方案中,我在打印前直接从华氏温度转换。 由@Scary Wombat建议我使用%。1f%n来显示摄氏温度,带有一个小数位和一个行分隔符。 更新:添加“\ u00b0F”和“\ u00b0C”以添加字符串°F和°C public static void main(String [] args){     // T(°C)=(T(°F) - 32)×5/9     for(int F = 120; F&gt; = -40; F- = 5){             System.out.printf(“% - 3d \ u00b0F%.1f \ u00b0C%n”,F,(double)((F-32d)* 5/9));     } }

答案 4 :(得分:-1)

这个怎么样:

// Each Celsius degree is calculated in the cycle.
double CasDouble = (F - 32.) * (5./9.);

// The double value is rounded to an integer
int C = (int) CasDouble;

// The application prints the Fahrenheit and Celsius degrees
System.out.printf("%-5d\t%-5d",F,C);

所以:

package chapter5;

public class Assignment2 {
    public static void main(String[] args) {
        // T(°C) = (T(°F) - 32) × 5/9
        int F = 120;
        int count = 0;

        while (F >= -40 && F <= 120) {
            if (F % 5 == 0) {

                // Each Celsius degree is calculated in the cycle.
                double CasDouble = (F - 32.) * (5. / 9.);

                // The double value is rounded to an integer
                int C = (int) CasDouble;

                // The application prints the Fahrenheit and Celsius degrees
                System.out.printf("%-5d\t%-5d", F, C);

                count++;
            }
            if (count == 1) {
                System.out.println();
                count = 0;
            }
            F--;
        }
    }
}