多个 类 博士 Java
multiple classes in Dr. Java
JDK 编译器说它可以编译,但是当它运行时我得到这个错误:
Static Error: This class does not have a static void main method accepting String[].
我习惯于将我的方法 class 放在一个文件中,而将主要的 class 放在一个单独的文件中。
我该如何解决这个问题?
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();
}}
- 一个文件只能有一个publicclass,多个
非public classes
- 您的文件名必须与唯一的 public
class
- 要使您的 class 可运行,它应该包含一个 public 静态
void main(String args[])方法,最好在publicclass
你的 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();
}
}
变化:
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
JDK 编译器说它可以编译,但是当它运行时我得到这个错误:
Static Error: This class does not have a static void main method accepting String[].
我习惯于将我的方法 class 放在一个文件中,而将主要的 class 放在一个单独的文件中。
我该如何解决这个问题?
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();
}}
- 一个文件只能有一个publicclass,多个 非public classes
- 您的文件名必须与唯一的 public class
- 要使您的 class 可运行,它应该包含一个 public 静态 void main(String args[])方法,最好在publicclass
你的 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();
}
}
变化:
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