2015-10-13 83 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... 
}