Java If -else语句未按预期工作

时间:2013-03-20 16:54:20

标签: java if-statement

所以我正在用相当低级别的Java编程创建一个程序。 这就是我遇到的问题:

//The String fillText is given a value earlier in the program
if ("".equals(txa1.getText()))
{
    txa1.setText(fillText);
    txa1.setVisible(true);
}
else if ("".equals(txa2.getText()))
{
    txa2.setText(fillText);
    txa2.setVisible(true);
}
else if ("".equals(txa3.getText()))
{
    txa3.setText(fillText);
    txa3.setVisible(true);
}
else if ("".equals(txa4.getText()))
{
    txa4.setText(fillText);
    txa4.setVisible(true);
}
else if ("".equals(txa5.getText()))
{
    txa5.setText(fillText);
    txa5.setVisible(true);
}
...

此代码似乎始终用fillText填充所有textareas(txaX)。 我希望它只执行返回true的第一个语句然后中断if-else语句。

我尝试用switch-case来做,但由于在程序运行期间更改了String,因此最终失败了。

有什么问题?

提前致谢!

3 个答案:

答案 0 :(得分:0)

"".equals(txa1.getText())

我认为每个条件都返回true。

getText()方法总是返回空字符串,即“”;

答案 1 :(得分:0)

它处于循环中。确定是导致问题。没有循环它将只到一个块。如果没有循环也不可能执行。当我们使用时,否则只执行一个块。

答案 2 :(得分:0)

你必须仔细检查你的条件,如果条件为假,它基本上会执行前辈。 我建议你更多地考虑你想要实现的逻辑......

相关问题