如何使我的循环正常工作以产生正确的输出?

时间:2014-11-19 17:43:01

标签: java

        public class p6
        {

            public static void main(String[] args)
            {
                System.out.println("celsius \t Fahrenheit \t | Fahrenheit \t\t Celsius");

                for (double i = 31; i <= 40; i++) {

        System.out.printf(i + "\t\t" + "\t" + "%.2f", +celsiusToFahrenheit(i));
        System.out.println("");
    }


    for (j = 30; j <= 120; j++) {

        System.out.printf("\t\t\t\t" +j + "\t\t\t\t" + "%.2f",+fahrenheitToCelsius(j));
        System.out.println("");
    }
                }
            }

            // • Converts a Celsius value to Fahrenheit
           // • Converts a Celsius value to Fahrenheit
            public static double celsiusToFahrenheit(double celsius)
            {
                return (9.0 / 5) * celsius + 32;
            }

            // Converts a Fahrenheit value to Celsius
            public static double fahrenheitToCelsius(double fahrenheit)
            {
                return (5.0 / 9) * (fahrenheit - 32);
            }
        }

这是输出,我得到但是我需要它全部按顺序排列。

celsius      Fahrenheit      | Fahrenheit        Celsius
31.0            87.80
32.0            89.60
33.0            91.40
34.0            93.20
35.0            95.00
36.0            96.80
37.0            98.60
38.0            100.40
39.0            102.20
40.0            104.00
                30.0                -1.11
                31.0                -0.56
                32.0                0.00
                33.0                0.56
                34.0                1.11
                35.0                1.67
                36.0                2.22
                37.0                2.78
                38.0                3.33
                39.0                3.89
                40.0                4.44
                41.0                5.00
                42.0                5.56
                43.0                6.11
                44.0                6.67
                45.0                7.22
                46.0                7.78
                47.0                8.33
                48.0                8.89
                49.0                9.44
                50.0                10.00
                51.0                10.56
                52.0                11.11
                53.0                11.67
                54.0                12.22
                55.0                12.78
                56.0                13.33
                57.0                13.89
                58.0                14.44
                59.0                15.00
                60.0                15.56
                61.0                16.11
                62.0                16.67
                63.0                17.22
                64.0                17.78
                65.0                18.33
                66.0                18.89
                67.0                19.44
                68.0                20.00
                69.0                20.56
                70.0                21.11
                71.0                21.67
                72.0                22.22
                73.0                22.78
                74.0                23.33
                75.0                23.89
                76.0                24.44
                77.0                25.00
                78.0                25.56
                79.0                26.11
                80.0                26.67
                81.0                27.22
                82.0                27.78
                83.0                28.33
                84.0                28.89
                85.0                29.44
                86.0                30.00
                87.0                30.56
                88.0                31.11
                89.0                31.67
                90.0                32.22
                91.0                32.78
                92.0                33.33
                93.0                33.89
                94.0                34.44
                95.0                35.00
                96.0                35.56
                97.0                36.11
                98.0                36.67
                99.0                37.22
                100.0               37.78
                101.0               38.33
                102.0               38.89
                103.0               39.44
                104.0               40.00
                105.0               40.56
                106.0               41.11
                107.0               41.67
                108.0               42.22
                109.0               42.78
                110.0               43.33
                111.0               43.89
                112.0               44.44
                113.0               45.00
                114.0               45.56
                115.0               46.11
                116.0               46.67
                117.0               47.22
                118.0               47.78
                119.0               48.33
                120.0               48.89

我需要在表中获取输出,但我现在遇到的问题是这些数字每次重复这么多次。循环确实停止了,我不需要得到什么。如果我使用不同的循环它工作正常但我需要输出两个不同的列。

我将摄氏温度转换为华氏温度,因此在一列中应显示摄氏温度,另一列应显示华氏温度。

    Then I am convering F to C, so in another column I need to display F and another the C.
    I need to get one column with celsius and another column with 

    The four columns have to be next to each other.

1 个答案:

答案 0 :(得分:3)

首先,您需要一个循环,而不是嵌套循环。

其次,为了使每个F到C转换在同一行中具有相应的C到F转换,ij必须具有相同数量的值。

这是一种可能的方法:

    double j = 30;
    for (double i = 31; i <= 40; i++) {
        System.out.print(i + "\t\t" + "\t" +celsiusToFahrenheit(i)+"\t\t\t" +j + "\t\t\t\t"+fahrenheitToCelsius(j));
        System.out.println("");
        j+=10; // since i has 10 values (31 to 40), j must increment by 10 in each 
               // iteration in order for it to have 10 values between 30 and 120
    }

输出:

celsius      Fahrenheit      | Fahrenheit        Celsius
31.0            87.80000000000001           30.0                -1.1111111111111112
32.0            89.6            40.0                4.444444444444445
33.0            91.4            50.0                10.0
34.0            93.2            60.0                15.555555555555557
35.0            95.0            70.0                21.11111111111111
36.0            96.8            80.0                26.666666666666668
37.0            98.60000000000001           90.0                32.22222222222222
38.0            100.4           100.0               37.77777777777778
39.0            102.2           110.0               43.333333333333336
40.0            104.0           120.0               48.88888888888889

它仍然需要对格式进行微小修复,但它非常接近你想要的。

更好的格式化:

System.out.println("celsius \tFahrenheit\t | \tFahrenheit \t\tCelsius");
double j = 30;
for (double i = 31; i <= 40; i++) {
    System.out.printf("%.2f\t\t%.2f\t\t\t%.2f\t\t\t%.2f", i, celsiusToFahrenheit(i),j,fahrenheitToCelsius(j));
    System.out.println("");
    j+=10; // since i has 10 values (31 to 40), j must increment by 10 in each 
           // iteration in order for it to have 10 values between 30 and 120
}

输出:

celsius     Fahrenheit   |  Fahrenheit      Celsius
31.00       87.80           30.00           -1.11
32.00       89.60           40.00           4.44
33.00       91.40           50.00           10.00
34.00       93.20           60.00           15.56
35.00       95.00           70.00           21.11
36.00       96.80           80.00           26.67
37.00       98.60           90.00           32.22
38.00       100.40          100.00          37.78
39.00       102.20          110.00          43.33
40.00       104.00          120.00          48.89