2011-09-30 33 views
3

我似乎無法得到此代碼正常工作。這是我不斷收到的錯誤:錯誤:無法找到或加載主類<<<爲什麼會出現此錯誤?

Error: Could not find or load main class.

是什麼原因造成的?

Payroll3.java

// A program to calculate and print the department, name, and pay of an employee. 
import java.util.Scanner; //program uses the Scanner class 
import java.io.*; 

class main 
{ 
public class Payroll3 

{ 
// main method begins execution of Java program. 
    public void main(String args[]) 
      { 
    // create Scanner to obtain input from the command window 
    Scanner input = new Scanner (System.in); 

    double number1; // first number to multiply 
    double number2; // second number to multiply 
    double product; // product of number1 and number2 


while(true){ // infinite loop 


    System.out.print("Enter Department name: "); //prompt 
    String name = input.nextLine(); // read name from user 

    if(name.equals("stop")) // exit the loop 
     break; 


    System.out.print("Enter number of employees: "); // prompt 
    number1 = input.nextDouble(); // read first number from user 
    input.nextLine(); 
     while(number1 <= -1){ 
     System.out.print("Enter positive number of employees:"); // prompt 
     number1 = input.nextDouble(); // read first number from user 
     input.nextLine(); 
     } /* while statement with the condition that negative numbers are entered 
     user is prompted to enter a positive number */ 

    System.out.print("Enter average salary: "); // prompt 
    number2 = input.nextDouble(); // read second number from user 
    input.nextLine(); 
     while(number2 <= -1){ 
     System.out.print("Enter positive number for average salary:"); // prompt 
     number2 = input.nextDouble(); // read first number from user 
     input.nextLine();      
} /* while statement with the condition that negative numbers are entered 
     user is prompted to enter a positive number */ 

    // make department object 
     Department dept = new Department(name,number1,number2); 

    product = number1 * number2; // multiply numbers 

    System.out.println("Department name:" + name); // display Department name 
    System.out.printf("Payroll is: $%.2f\n", product); // display product 

    } // end while method 

} // end method main 

}/* end class Payroll3 */ 
} 

// Stores data about an department 
class Department { 

//fields 
String name; 
double number1; 
double number2; 

// constructor 
public Department(String name, double number1, double number2) { 
    this.name = name; 
    this.number1 = number1; 
    this.number2 = number2; 
} 

// returns the pay: 
public double getPay() { 
    return number1*number2; 
} 

// getters and setters 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public double getnumber1() { 
    return number1; 
} 

public void setNumber1(double number1) { 
    this.number1 = number1; 
} 

public double getNumber2() { 
    return number2; 
} 

public void setNumber2(double number2) { 
    this.number2 = number2; 
} 


} 
+0

爲什麼你有Payroll3內的外部主類?你應該刪除它。 –

回答

9

類必須publicmain必須是靜態的

public static void main(String argv[]) 

而且你不能嵌套主類。至少它可能不是你所期望的。你爲什麼不從簡單的教程開始,並用你的代碼進行擴展?

0

問題是需要將main函數聲明爲public static void main(String[] args)而不是public void main(String[] args)

+0

我做了這個改變,但我無法讓事情工作。我在嵌套和嵌套主類時嘗試了它。 –

1

更詳細的要求

你遵守了嗎?

Error: Could not find or load main class.?

看來你剛與源文件

java test.java # wrong 
Exception in thread "main" java.lang.NoClassDefFoundError: test/java 
Caused by: java.lang.ClassNotFoundException: test.java 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266) 
Could not find the main class: test.java. Program will exit. 

# correct 
javac yourfile.java 
java yourfile 

而且你需要運行Java來改變你的文件作爲亞歷克斯Gitelman說。

+0

我已編譯它,但我仍然收到錯誤 –

+0

@NetanaBranham:我沒有注意到外層'main main {',你可以刪除它,並將'public class Payroll3'作爲最外層的類,使用'public static void main(String argv [])'裏面並接受來自@AlexGitelman的答案? –

0

編譯時應使用該文件的名稱,即javac test.java,其中test.java是文件名。而當運行該文件的類名稱是文件應該用於例如,如果它是類測試{}然後java測試

1

我簡化你的代碼,揭示裏面的問題。

// Payroll3.java 

class main 
{ 
public class Payroll3 
{ 
    public void main(String args[]) 
      { 

      } 
} 
} 

首先,我們可以看到最外層的可疑class main隱藏你的需要類和主要功能。您需要刪除class main

其次,主函數需要聲明爲類函數而不是實例函數,因爲它沒有綁定到任何實例。在java中,我們使用static來聲明一個類變量/類函數。所以,你需要聲明mainpublic static void main(String argv[])

更改後,就可以得到:

// Payroll3.java 

public class Payroll3 
{ 
    public static void main(String argv[]) 
    { 

    } 
} 
+0

我只是嘗試添加更多的細節。 @AlexGitelman只是顯示原因,但沒有直接解決的方法。 –

2

同時運行程序只需鍵入java program_namejava program_name.java

相關問題