使用Array的温度转换不起作用?

时间:2015-10-31 22:04:12

标签: java

所以我试图将数组中的数字从fahreneheit转换为celcius;我没有收到任何错误,但我的数字绝对不对。这是我的代码:

package lab7q2;

public class Lab7Q2 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here


    int[] tempList = {212, 95, 64, 32, 0, -12, -25};

    for (int i = 0; i < tempList.length; i++) {
        System.out.format("temp in F %4d  temp in C %6.2f \n", + tempList[i], tempFtoC(i));
    }


}

public static double tempFtoC (int tempList) {

    double tempC = ((tempList - 32)*5)/9;
    return tempC;
    }

}

这是我的输出:

temp in F  212  temp in C -17.00 
temp in F   95  temp in C -17.00 
temp in F   64  temp in C -16.00 
temp in F   32  temp in C -16.00 
temp in F    0  temp in C -15.00 
temp in F  -12  temp in C -15.00 
temp in F  -25  temp in C -14.00 
BUILD SUCCESSFUL (total time: 0 seconds)

任何人都可以告诉我为什么它没有做正确的转换?我的方法设置不正确吗? (编程新手,所以请不要对我太粗暴了......)

1 个答案:

答案 0 :(得分:2)

您要转换i,而不是tempList[i],因此您应该将格式声明更改为

System.out.format("temp in F %4d  temp in C %6.2f \n", + tempList[i], tempFtoC(tempList[i]));