gpt4 book ai didi

java - if-else 语句中的混淆

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:27 24 4
gpt4 key购买 nike

好吧,我对 if-else 语句有点困惑。我的表格布局中有 4 行,每行都会被检查以查看它是否有值(value)。

案例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 和 W4!=null,但我得到第 1 行仅具有值

最佳答案

至少代码末尾必须有右大括号。

无论如何,我看到了 if-s 的结构:

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

也许您希望代码 4 的“else”引用条件 2 的“if”,而不是条件 3 的“if”?然后你可以用这种方式改变结构(在其他之前添加}):

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

关于java - if-else 语句中的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33109569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com