嵌套if语句 - 变量可能尚未初始化

时间:2014-04-15 19:10:21

标签: java if-statement

我知道错误可能在if语句中,但是我想如何编写它以使它可以工作?

该程序假设您知道如果您有“i”房间,您必须支付护士服务费用,但如果您有“d”房间,则必须支付电话和电视服务费用。

// 处理

        if (room == "I") { // Begin if (room == "I")

            if (nurse == "X") { // begin if (nurse == "X")

                roomCost = (double)days * I_COST;
                TVCost = X_COST;
                phoneCost = X_COST;
                nurseCost = (double)days * X_COST;

            } // end if (nurse == "X")

            else if (nurse == "S"){ // begin else if (nurse == "S")

                roomCost = (double)days * I_COST;
                TVCost = X_COST;
                phoneCost = X_COST;
                nurseCost = (double)days * S_COST;

            } // end else if (nurse == "S")

            else if (nurse == "N") { // begin else if (nurse == "N")

                roomCost = (double)days * I_COST;
                TVCost = X_COST;
                phoneCost = X_COST;
                nurseCost = (double)days * N_COST;

            } // end else if (nurse == "N")

        }  // end if (room == "I")  



         if (room == "D") { // Begin  if (room == "D")

            if (TV == "V" && phone == "T") { // begin if (TV == "V" && phone == "T")

                roomCost = (double)days * D_COST;
                nurseCost = X_COST;
                TVCost = (double)days * V_COST;
                phoneCost = (double)days * T_COST;

            } // end if (TV == "V" && phone == "T")

            else if (TV == "V" && phone == "X") { // begin else if (TV == "V" && phone == "X")

                roomCost = (double)days * D_COST;
                nurseCost = X_COST;
                TVCost = (double)days * V_COST;
                phoneCost = X_COST;

            } // end else if (TV == "V" && phone == "X")

            else if (TV == "X" && phone == "T") { // begin else if (TV == "X" && phone == "T")

                roomCost = (double)days * D_COST;
                nurseCost = X_COST;
                TVCost = X_COST;
                phoneCost = (double)days * T_COST;

            } // end else if (TV == "V" && phone == "X")

            else if (TV == "X" && phone == "X") { // begin else if (TV == "X" && phone == "X")

                roomCost = (double)days * D_COST;
                nurseCost = X_COST;
                TVCost = X_COST;
                phoneCost = X_COST;

            } // end else if (TV == "X" && phone == "X")

        }  // end  if (room == "D") 

    // ********** OUTPUT **********

        // df1.format(double) ends up giving me two decimal places

        System.out.println("\nPatient ID     Days     Room       Nurse     TV Service     Telephone Service");
        System.out.println(patientID + "\t\t   " + days + "\t    $" + df1.format(roomCost) + "\t $" + df1.format(nurseCost) + "\t   $" + df1.format(TVCost) + "\t\t\t  $" + df1.format(phoneCost)); // ERROR VARIABLE ROOMCOST, NURSECOST, TVCOST AND PHONE COST MIGHT NOT HAVE BEEN INITALIZED

2 个答案:

答案 0 :(得分:3)

使用.equals()方法进行字符串相等性检查。 ==仅在基元的情况下是好的,但是在对象的情况下,它实际上检查两个变量是否引用同一个对象。

所以你的条件应该是

if (room.equals("I")) { 

对于警告,变量roomCostnurseCostTVCostphoneCost之类的声音可以以0.0的值开头,这样你就可以至少摆脱警告。

答案 1 :(得分:0)

如果在方法中声明了roomCostnurseCostTVCostphoneCost,则默认情况下不会对其进行初始化。我通常使用的是初始化为默认值,我不会使用。

例如,这不会起作用:

void foo()
{
 int i;
 int j = 7;

 if(j==7)
 {
  i=3;
 }
 else
 {
  i=2;
 }
  if (i==2)
 {
  System.out.println("it works");
 }
}

但是,如果您还要将i初始化为-1,例如,它将:

int i = -1;