Java 而循环写得太难了

Java 而循环写得太难了,java,Java,我写了这段代码,它很有效,但我似乎找不到一种更简单的方法来写它。现在我正在指定所有可能的情况,并为它们添加函数。如果我只使用两个布尔变量(status1和status2),这是可行的,就像下面的代码一样。但是如果我使用两个以上的变量,那么我需要编写的代码就太多了 while (!status1 || !status2) { if (!status1 && !status2) { jTextField1.setForegrou

我写了这段代码,它很有效,但我似乎找不到一种更简单的方法来写它。现在我正在指定所有可能的情况,并为它们添加函数。如果我只使用两个布尔变量(status1和status2),这是可行的,就像下面的代码一样。但是如果我使用两个以上的变量,那么我需要编写的代码就太多了

while (!status1 || !status2) {
            if (!status1 && !status2) {
                jTextField1.setForeground(Color.red);
                jTextField2.setForeground(Color.red);
                break;
            } else if (!status1 && status2) {
                jTextField1.setForeground(Color.red);
                break;
            } else if (status1 && !status2) {
                jTextField2.setForeground(Color.red);
                break;
            }
基本上,我想要实现的是编写如下代码(不指定所有可能的情况)。我测试了这段代码,但它只执行第一个if语句,而不执行其他语句

我做错了什么?我想让它遍历所有的if语句

while (!status1 || !status2) {
            if (!status1 {
                jTextField1.setForeground(Color.red);
                break;
            } else if (!status2) {
                jTextField2.setForeground(Color.red);
                break;
            }

另一个问题是你的问题。中断也不正确

if (!status1) {
    jTextField1.setForeground(Color.red);
} 
if (!status2) {
    jTextField2.setForeground(Color.red);
}

看不出为什么要使用
while
循环。像这样的怎么样

Color color1 = Color.red;
Color color2 = Color.red;

if (status1) {
    color1 = 0;
}

if (status2) {
    color2 = 0;
}

jTextField1.setForeground(color1);
jTextField2.setForeground(color2);

我认为这是因为你在“while”语句中写的语句与第一个“if”语句相同。如果你写信

while(true)

相反,它应该起作用

while (!status1 || !status2 /* || !status3 || and so on*/) {
    boolean do_break=false;
    if (!status1) {
        jTextField1.setForeground(Color.red);
        do_break=true;
    } 
    if (!status2) {
        jTextField2.setForeground(Color.red);
        do_break=true;
    }
/*    if (!status3) {
        jTextField3.setForeground(Color.red);
        do_break=true;
    }*/
    if(do_break) break;

    here_the_part_that_you_omitted_from_the_question();//...
}

请澄清是否存在您没有告诉我们的循环体。

以下是简化的等效代码 您的代码:

while (!status1 || !status2) {
            if (!status1 && !status2) {
                jTextField1.setForeground(Color.red);
                jTextField2.setForeground(Color.red);
                break;
            } else if (!status1 && status2) { 
                jTextField1.setForeground(Color.red);
                break;
            } else if (status1 && !status2) {
                jTextField2.setForeground(Color.red);
                break;
            }
}
在上述代码中,最后一个“if”根本不需要!当我们在while循环中时,肯定至少有两个循环中的一个是错误的,如果控制来到这里,那么最重要的“如果”必须失败!在这种情况下,这个if条件永远不会失败

    while (!status1 || !status2) {
            if (status1) {                //equivalent to (status1 && !status2) 
                jTextField2.setForeground(Color.red);
                break;

            } else if(status2){           //equivalent to (!status1 && status2) 
                jTextField1.setForeground(Color.red);
                break;
            }
                jTextField2.setForeground(Color.red);
                jTextField1.setForeground(Color.red);            
                break;
}
同样的结果,但很简单:试试看

    while (!status1 || !status2) {
            if (status1) {                //equivalent to (status1 && !status2) 
                jTextField2.setForeground(Color.red);
                break;

            } else if(status2){           //equivalent to (!status1 && status2) 
                jTextField1.setForeground(Color.red);
                break;
            }
                jTextField2.setForeground(Color.red);
                jTextField1.setForeground(Color.red);            
                break;
}

谢谢你的帮助,但是使用这段代码会使我的程序不稳定(好像它在一个无限循环中或是其他什么东西),这就是为什么我在每个if语句之后使用中断。如果我添加中断,它将只执行第一个If语句。对此我们能做些什么?你是对的,为什么会有循环?。如果有,请在If语句后加上分隔符。