2015-10-13 79 views
1

好吧,我对if-else statement有点困惑。我在我的table layout中有4行,并检查每行是否有价值。if-else语句混淆

案例1

public void onClick(DialogInterface dialog, int ii) 
{ 
    if ((i != null && i.trim().length() > 0)) 
    { 
     WD.insertWorkDetails(a1, W1, P1, b, c); //row 1 
     WD.insertWorkDetails(a2, W2, P2, d, e1); //row 2 
     WD.insertWorkDetails(a3, W3, P3, f, g); //row 3 
     WD.insertWorkDetails(a4, W4, P4, h, i); //row 4 
     ts.insertTimeSheet(name, weather, date, status, b, i); 
     WF.insertWorkForce(subContractors, noPeople, noHours); 
     Toast.makeText(getApplicationContext(), "4 Row has value", Toast.LENGTH_LONG).show(); 
    } 
    else if (TextUtils.isEmpty(i) && (g != null && g.trim().length() > 0)) 
    { 
     if ((TextUtils.isEmpty(W4)) && (TextUtils.isEmpty(P4)) && (TextUtils.isEmpty(h)) && (TextUtils.isEmpty(i))) 
     { 
      WD.insertWorkDetails(a1, W1, P1, b, c); 
      WD.insertWorkDetails(a2, W2, P2, d, e1); 
      WD.insertWorkDetails(a3, W3, P3, f, g); 
      ts.insertTimeSheet(name, weather, date, status, b, g); 
      WF.insertWorkForce(subContractors, noPeople, noHours); 
      Toast.makeText(getApplicationContext(), "Row 4 no value", Toast.LENGTH_LONG).show(); 
     } 
     else 
     { 
      database = dbHelper.getWritableDatabase(); 
      WD.insertWorkDetails(a1, W1, P1, b, c); 
      WD.insertWorkDetails(a2, W2, P2, d, e1); 
      WD.insertWorkDetails(a3, W3, P3, f, g); 
      WD.insertWorkDetails(a4, W4, P4, h, i); 
      ts.insertTimeSheet(name, weather, date, status, b, g); 
      WF.insertWorkForce(subContractors, noPeople, noHours); 
      Toast.makeText(getApplicationContext(), " All Row has value ", 
      Toast.LENGTH_LONG).show(); 
       } 
} 

在案例1中,g!=nullw4持有价值,但我得到Row 4 no value其假设返回All Row has value。为什么?

案例2

else if (TextUtils.isEmpty(i) && (TextUtils.isEmpty(g) && (TextUtils.isEmpty(e1)) && (c != null && c.trim().length() > 0))) 
{ 
    if ((TextUtils.isEmpty(W4)) && (TextUtils.isEmpty(P4)) 
     && (TextUtils.isEmpty(h)) && (TextUtils.isEmpty(i)) 
     && ((TextUtils.isEmpty(W3)) && (TextUtils.isEmpty(P3)) 
     && (TextUtils.isEmpty(f)) && (TextUtils.isEmpty(g))) 
     && ((TextUtils.isEmpty(W2)) && (TextUtils.isEmpty(P2)) 
     && (TextUtils.isEmpty(d)) && (TextUtils.isEmpty(e1)))) 
    { 
     ts.insertTimeSheet(name, weather, date, status, b, c); 
     WF.insertWorkForce(subContractors, noPeople, noHours); 
     WD.insertWorkDetails(a1, W1, P1, b, c); 
     Toast.makeText(getApplicationContext(), "Row 1 has value only", Toast.LENGTH_LONG).show(); 
    } 
    else if ((W4 != null && W4.trim().length() > 0)) 
    { 
     WD.insertWorkDetails(a1, W1, P1, b, c); 
     WD.insertWorkDetails(a2, W2, P2, d, e1); 
     WD.insertWorkDetails(a3, W3, P3, f, g); 
     WD.insertWorkDetails(a4, W4, P4, h, i); 
     ts.insertTimeSheet(name, weather, date, status, c, b); 
     WF.insertWorkForce(subContractors, noPeople, noHours); 
     Toast.makeText(getApplicationContext(), "All row have value", Toast.LENGTH_LONG).show(); 
     } 
} 

在案例2中,c and W4!=null,但我得到row 1 has value only

+0

没问题,你错过了一些右括号,我假设他们走的尽头,但留下他们以防万一。 – DoubleDouble

+0

你是否在这个代码中可见的地方使用了一个变量'i'?只是注意到这个方法使用'ii'不知道在移动代码时这是否是编码错误或错字 - 'onClick(DialogInterface对话框,int ii)' - 可能是为什么'i'不是null,而是进入case 1中的第一个'if'语句 – DoubleDouble

+0

@DoubleDouble我正在使用Alert Dialog..The ii和i不一样:) – John

回答

0

至少必须在代码末尾加上大括号。

反正我看-S的这种结构:

if(...condition-1...) { 
    ...code-1... 
} esle if(...condition-2...) { 
    if(...condition-3...){ 
    ...code-3... 
    } else { 
    ...code-4... 
    } 
} 

也许你想“其他人”的代码-4是指“如果”与条件-2,而不是“如果”与条件-3?那么你会改变结构以这种方式(另外)在其他之前):

if(...condition-1...) { 
    ...code-1... 
} esle if(...condition-2...) { 
    if(...condition-3...){ 
    ...code-3... 
    } 
} else { 
    ...code-4... 
}