我得到的錯誤是:錯誤:無法找到或加載主類 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);
}
}
uj5u.com熱心網友回復:
每當在該網站上編譯 Java 代碼時,該網站的編譯器都會查找名為“Main”的類以開始執行。一旦其編譯器找到該類,它就會呼叫任何具有public static void main(String[] args). 在您的情況下,沒有名為“Main”的類,因此編譯器不知道從哪里開始運行程式。一個簡單的解決方法是將類名“Registration”更改為“Main”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/471686.html
下一篇:使用狀態模式了解物件的狀態
