程序失败if语句

时间:2016-03-04 13:29:00

标签: java

我正在完成一项任务,并想出了这段代码。我有所有正确的公式等。我运行调试器,看到我的程序跳过我的第一个if语句。我不知道为什么继续这样做。我一直在研究这个程序几个小时,但找不到解决方案。

public static void main (String args[])
{
    Scanner inFile = null; //creating scanner
    try
    {
        inFile = new Scanner(new File("prog435t.dat")); //reading file
    }
    catch (FileNotFoundException e) //if no file
    {
        System.out.println("File not found!");
        System.exit(0);
    }

    String [] sex = new String [12] ;
    String[] bone = new String [12];
    int [] age = new int [12];
    double [] length = new double [12];

    for(int count = 0; count<12; count++)
    {
        sex[count] = inFile.next();           
        bone [count] =  inFile.next();
        age [count] = inFile.nextInt();
        length [count] = inFile.nextDouble();
    }

    double [] height = new double [12];

    for(int count = 0; count<12; count++)
    {
        if(sex[count] == "M")
        {
            switch(bone[count])
            {
                case "F":
                            height[count] = 69.089 + 2.238 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "T":
                            height[count] = 81.688 + 2.392 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "H": 
                            height[count] = 73.570 + 2.970 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "R":
                            height[count] = 80.405 + 3.650 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                default:
                            height[count] = 0;
                            break;

            }
        }

        else 
        {
            switch(bone[count])
            {
                case "F":
                            height[count] = 61.412 + 2.317 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "T":
                            height[count] = 72.572 + 2.533  * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "H": 
                            height[count] = 64.977 + 3.144 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                case "R":
                            height[count] = 73.502 + 3.876 * length[count];
                            height[count] = (int)(height[count] *1000 +.5)/1000.0; //rounding
                            break;
                default:
                            height[count] = 0;
                            break;                                
            }
        }

        if(age[count] >30)
        {
            double newAge = age[count]-30;

            height[count] = height[count] - (0.06*newAge);
        }


    }

    for(int count = 0; count < 12; count++)
    {

    }

    System.out.println("Sex\tBone\tAge\tLength\t    Height\n");
    for(int count = 0; count<12; count++)
    {
        System.out.print(sex[count] + "\t" + bone[count] + "\t" + age[count] + "\t" + length[count] + "\t    " + height[count] +"\n");
    }
}

我的数据如下:

F H 45 36.266
M F 18 46.967
M H 29 33.309
Q F 40 30.078
M T 95 36.983
M R 30 23.1768
F F 31 42.55
M Q 40 30
F F 65 39.96
F T 50 30.962
F R 80 19.375
M H 36 24.017

这是所需的输出:

sex bone    age length  height
F   H   45  36.266  178.097
M   F   18  46.967  174.201
M   H   29  33.309  172.498
M   T   95  36.983  166.251
M   R   30  23.177  165.000
F   F   31  42.550  159.940
F   F   65  39.960  151.899
F   T   50  30.962  149.799
F   R   80  19.375  145.600
M   H   36    24.017    144.540
Q   F   40  30.000  Wrong Sex Code
M   Q   40  30.000  Wrong Bone Code

1 个答案:

答案 0 :(得分:0)

使用equals代替==来比较String个对象(比较值)。 也:

"M".equals(sex[count])

或者另一种方式,如果您还检查护理性[计数]不是Null