在if-else语句中混淆

时间:2015-10-13 18:03:32

标签: java if-statement

好吧,我对if-else statement感到有些困惑。我的table layout中有4行,每行都会检查它是否有价值。

案例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

2 个答案:

答案 0 :(得分:0)

至少必须在代码末尾关闭大括号。

无论如何,我看到了if-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...
}

答案 1 :(得分:0)

我认为,if-else语句没有问题。可能是,你得到长度为>的空字符串; 0. TextUtils.isEmpty():如果字符串为null或0-length,则返回true。 http://developer.android.com/reference/android/text/TextUtils.html#isEmpty(java.lang.CharSequence)