java else 没有 if 错误

标签 java

我用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 时,您不应将结果与 truefalse 进行比较。只需编写 if (Validate())...if (!Validate())... 即可。

关于java else 没有 if 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14805680/

相关文章:

JavaMail,如果接收者邮件错误,不会抛出异常

java - 如何将H2数据库嵌入到传递给客户端的jar文件中?

java - 如何从另一个类调用存储在 applicationScope 中的 ArrayList

java - 在 Eclipse 上构建 Apache Spark 的依赖项

java - 从字节数组转换和显示图像

java - Java 字符串中的反斜杠问题

java - JEE7 @Transactional 注释并不总是触发

java - 是否可以使用java读取传入数据流,从服务器到.exe客户端(使用C++编码)?

Activity onCreate 中的 java.util.ConcurrentModificationException

Java多线程: cheap operation to add object to List/Queue/Set