以下程序過早退出while循環。如果我拿出||
只剩下一個選項離開循環,那麼它工作正常。但是,我但我不想要這個;我希望有多種條件擺脫循環,而不僅僅是一個。多部分環路條件不應該是
public class password
{
public static void main (String args[])
{
new password();
}
public password()
{
String guess = "";
//while they haven't got the password yet
while (!guess.equals ("Rain") || !guess.equals ("rain"))
{
guess = IBIO.inputString ("Enter the password: ");
if (!guess.equals ("Rain") || !guess.equals ("rain"))
{
System.out.println ("Sorry please try again.");
}
}
//if they are out of the loop, they got the password
System.out.println ("Correct, please continue");
}
}
如果你打算使用無括號的'if'語句,至少應該把語句放在'if'語句附近,否則你會在'if'和'System.out.println之間意外地輸入另一個語句'因爲括號內沒有明確的說明。在Java中總是使用括號和if語句通常是一個好習慣。我有時不會,但我確定它非常清楚。將它傳播出去會讓它變得更加混亂/難看。 – Matthew