[英]Confuse in if-else statement
ok, i get a little bit confused on the if-else statement
. 好的,我对
if-else statement
有点困惑。 I have 4 row in my table layout
and each row will be checked to see whether it has value or not. 我的
table layout
有4行,将检查每一行以查看其是否有价值。
Case 1 情况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();
}
}
In case 1, g!=null
and w4
holds a value, but I get Row 4 no value
which suppose to return All Row has value
. 在情况1中,
g!=null
并且w4
拥有一个值,但是我得到Row 4 no value
,该Row 4 no value
应该返回All Row has value
。 Why? 为什么?
Case 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();
}
}
In case 2, c and W4!=null
, but I get row 1 has value only
. 在情况2中,
c and W4!=null
,但是我得到row 1 has value only
。
At least there must be to closing braces at the end of your code. 至少在代码末尾必须有大括号。
Anyway, I see this structure of if-s: 无论如何,我看到了if-s的结构:
if(...condition-1...) {
...code-1...
} esle if(...condition-2...) {
if(...condition-3...){
...code-3...
} else {
...code-4...
}
}
Maybe You want "else" with code-4 to refer to "if" with condition-2, not to "if" with condition-3? 也许您希望代码4的“其他”引用条件2的“ if”,而不是条件3的“ if”? Then You would change the structure in this way (additional } before else):
然后,您将以这种方式更改结构(在其他位置之前使用}):
if(...condition-1...) {
...code-1...
} esle if(...condition-2...) {
if(...condition-3...){
...code-3...
}
} else {
...code-4...
}
I think, There is no issue with if-else statement. 我认为,if-else语句没有问题。 May be, you are getting empty string with length > 0. TextUtils.isEmpty() : Returns true if the string is null or 0-length.
可能是,您正在获取长度大于0的空字符串。TextUtils.isEmpty():如果字符串为null或长度为0,则返回true。 http://developer.android.com/reference/android/text/TextUtils.html#isEmpty(java.lang.CharSequence)
http://developer.android.com/reference/android/text/TextUtils.html#isEmpty(java.lang.CharSequence)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.