代码之家  ›  专栏  ›  技术社区  ›  LawlessTJ

由于错误,我的Java代码没有运行,如何修复它以使其运行

  •  0
  • LawlessTJ  · 技术社区  · 2 年前

    我得到的错误是: 错误:无法找到或加载main类main 原因:java。lang.ClassNotFoundException:Main

    我是Java新手,不知道在这个实例中该做什么,也不知道如何修复代码以运行

    由于错误描述,我认为这与类有关,但我不确定

    import java.util.Scanner;
    import java.util.regex.*;
    
    class Login{
        String user;
        String password;
        String firstName;
        String lastName;
    
        boolean checkUserName(String user){
            if(user.length()>5 || user.indexOf('_')==-1){
                return false;
            }
            else{
                return true;
            }
        }
    
        boolean checkPasswordComplexity(String password){
            String regex = "^(?=.*[0-9])"
                           + "(?=.*[a-z])(?=.*[A-Z])"
                           + "(?=.*[@#$%^&+=])"
                           + "(?=\\S+$).{8,20}$";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(password);
            return m.matches();
        }
    
        boolean loginUser(String user,String password){
            if(user.equals(this.user) && password.equals(this.password)){
                return true;
            }
            else{
                return false;
            }
        }
    
        String returnLoginStatus(String user,String password){
            if(loginUser(user, password)){
                return "Welcome "+firstName+lastName+" it is greater to see you again.";
            }
            else{
                return "Username or passoword incorrect, please try again.";
            }
        }
    
        void registerUser(String firstName,String lastName,String user,String password){
    
            if(checkUserName(user)){
                this.user = user;
                System.out.println("Username successfully Captured");
            }
            else{
                System.out.println("Username is not correctly formatted, please ensure that your username contains an underscore and is no more than 5 characters long.");
            }
           if(checkPasswordComplexity(password)){
               this.password = password;
                System.out.println("Password successfully captured");
            }
            else{
                System.out.println("Password is not correctly formatted, please ensure that the passoword contains atleast 8 characters, a capital letter, a number and a special character.");
            }
            if(checkUserName(user) && checkPasswordComplexity(password)){
                this.firstName = firstName;
                this.lastName = lastName;
                System.out.println(returnLoginStatus(user, password));
            }
        }
    }
    
    class Registration{
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            
            System.out.print("Enter the username:");
            String user = sc.nextLine();
            System.out.print("Enter the password:");
            String password = sc.nextLine();
            System.out.print("Enter your First Name:");
            String firstName = sc.nextLine();
            System.out.print("Enter Your Last Name:");
            String lastName = sc.nextLine();
            sc.close();
            Login user1 = new Login();
            user1.registerUser(firstName, lastName, user, password);
    
        }
       
        
    }
    
    1 回复  |  直到 2 年前
        1
  •  -1
  •   s_em_pervirens    2 年前

    每当编译Java代码时,编译器都会寻找一个名为“Main”的类来开始执行。一旦编译器找到该类,它将调用任何具有 public static void main(String[] args) .在你的例子中,没有名为“Main”的类,所以编译器不知道从哪里开始运行程序。解决这个问题的一个简单方法是将类名“Registration”更改为“Main”。