我用java写了这个,并且不断得到else而没有if错误。我检查了一遍又一遍,找不到错误。如果程序的其余部分有问题,这真的是缺失吗?
public class Password {
public static void main(String[] args) {
//int Counter = 0;
String[] UserName = {
"William Whitcomb" , "Pamela Healy" ,
"Dennis Clark" , "Troy Bingham" ,
"Bill Mauler"
};
String[] Password = {
"WWhit0523" , "PHeal0854" ,
"DClar1053" , "TBing1272" ,
"BMaul0968"
};
String EnterName = JOptionPane.showInputDialog ( "Enter a valid user name.", //Name pane window
"User Name");
String EnterPassword = JOptionPane.showInputDialog ( "Enter a valid password.", //Age pane window
"Password");
for (int Counter = 0; Counter < UserName.length;){
Validate();
if (Validate() = true) {
JOptionPane.showMessageDialog (null, "You entered the User Name of " + UserName[Counter] +
"and the password of " + Password[Counter]); //Results pane
}
} //End of for
} //End of Method
Boolean Validate(String EnterName , String EnterPassword){
Boolean Condition = false;
if (EnterName.equals(UserName[Counter])) {
if (EnterPassword.equals(Password[Counter])) {
Condition = true;
return //Condition;
} else {
JOptionPane.showMessageDialog (null, "You have entered an invalid password.");
Counter += 1;
} //End of Inner Else
} else {
JOptionPane.showMessageDialog (null, "You have entered an invalid user name.");
Counter += 1;
} //End of Outer Else
} //End of outer if
} //End of Method
} //End of Class
最佳答案
我认为问题出在这一行:
return //Condition;
您不小心注释掉了分号,从而引发了不相关的解析错误。
在某种程度上相关的说明中,当您的函数返回 boolean
时,您不应将结果与 true
或 false
进行比较。只需编写 if (Validate())...
或 if (!Validate())...
即可。
关于java else 没有 if 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14805680/