JDK编译器说它可以编译,但是在运行时出现此错误:
静态错误:此类没有接受String []的静态void main方法。
我习惯于将方法类放在一个文件中,而将主类放在一个单独的文件中。
我该如何解决这个问题?
import java.util.Scanner;
public class Test{
private final int classSize = 35;
private int numEnrolled, numNeeded;
public void Input(){
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount enrolled in your class");
numEnrolled = in.nextInt();
System.out.println("Your input is " + numEnrolled);
// Other parts of code that needs to be coded
}
}
class testRunner{
static void main(String args[]){
Test newTest = new Test();
newTest.Input();
}}
答案 0 :(得分:1)
您的TestRunner.java应该看起来像这样
class Test{
private final int classSize = 35;
private int numEnrolled, numNeeded;
public void Input(){
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount enrolled in your class");
numEnrolled = in.nextInt();
System.out.println("Your input is " + numEnrolled);
// Other parts of code that needs to be coded
}
}
public class TestRunner{
public static void main(String args[]){
Test newTest = new Test();
newTest.Input();
}}
编辑: 如果要保留文件名Test.java,这也可以:
public class Test {
private final int classSize = 35;
private int numEnrolled, numNeeded;
public void Input() {
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount enrolled in your class");
numEnrolled = in.nextInt();
System.out.println("Your input is " + numEnrolled);
// Other parts of code that needs to be coded
}
}
class TestRunner {
public static void main(String args[]) {
Test newTest = new Test();
newTest.Input();
}
}
答案 1 :(得分:0)
更改:
class testRunner{
static void main(String args[]){
Test newTest = new Test();
newTest.Input();
}}
收件人:
public class testRunner {
public static void main(String args[]) {
Test newTest = new Test();
newTest.Input();
}}
main()
必须始终声明为 public