Java 定义main方法为:public static void main(String[] args)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23898030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define the main method as: public static void main(String[] args)
提问by user3681220
Can someone please help me with the code below it builds without any error but when i Run it i get "Error: Main method not found in class Program, please define the main method as: public static void main(String[] args)"...
有人可以帮助我使用下面的代码构建而没有任何错误,但是当我运行它时,我得到“错误:在类 Program 中找不到 Main 方法,请将主要方法定义为:public static void main(String[] args)” ...
any ideas why ?
任何想法为什么?
import java.util.*;
public class Program
{
// Create an array/database for 100 prizes We can increase this number
private Prize[] prizeDatabase = new Prize[100];
// variable to store the next available position in the database
private int currentArrayPosition = 0;
/**
This method displays the menu to the user
*/
private void DisplayMenu()
{
System.out.println("Please select an option from the following:");
System.out.println("1. Enter details of a prize");
System.out.println("2. Print the details stored for all prizes");
System.out.println("3. Search for a prize by description or value");
System.out.println("4. Quit");
// Get the users selection
String selection = new Scanner(System.in).nextLine();
if (selection.equals("1"))
{
EnterPrizeDetails();
}
else if (selection.equals("2"))
{
PrintPrizes();
}
else if (selection.equals("3"))
{
SearchPrize();
}
else if (selection.equals("4"))
{
// do nothing, console will exit automatically
}
}
/**
Search a prize
*/
private void SearchPrize()
{
System.out.println("Please enter search term and press enter: ");
// get the users search term from the console
String searchTerm = new Scanner(System.in).nextLine();
System.out.println("Your following matches are: ");
// Loop round all the prizes
for (Prize prize : prizeDatabase)
{
if ((prize != null))
{
// if the prize matches the users search criters then print the prize
if (prize.MatchPrize(searchTerm))
{
prize.PrintPrize();
}
}
}
System.out.println();
DisplayMenu();
}
/**
Print all prizes in the database
*/
private void PrintPrizes()
{
System.out.println("The following prizes are in the database: ");
for (Prize prize : prizeDatabase)
{
if (prize != null)
{
prize.PrintPrize();
}
}
System.out.println();
DisplayMenu();
}
/**
Enter a prize and store it into the database
*/
private void EnterPrizeDetails()
{
// Take user input and store it to the enter the prize into the database
System.out.println("Please enter a description of the prize and then enter");
String description = new Scanner(System.in).nextLine();
System.out.println("Please enter the colour of the prize and then enter");
String color = new Scanner(System.in).nextLine();
System.out.println("Please enter a value of the prize and then enter");
String value = new Scanner(System.in).nextLine();
Prize newPrize = new Prize();
newPrize.SetPrizeDetails(description, color, value);
// Check if we can add the prize to our database
if (currentArrayPosition < 100)
{
prizeDatabase[currentArrayPosition] = newPrize;
currentArrayPosition++;
System.out.println("A prize has successfully been added to the database.");
}
else
{
System.out.println("Please contact admin to increase the size of the database");
}
System.out.println();
DisplayMenu();
}
static void main(String[] args)
{
Program program = new Program();
program.DisplayMenu();
}
}
class Prize extends Program
{
private String privateDescription;
private String getDescription()
{
return privateDescription;
}
private void setDescription(String value)
{
privateDescription = value;
}
private String privateColor;
private String getColor()
{
return privateColor;
}
private void setColor(String value)
{
privateColor = value;
}
private String privateValue;
private String getValue()
{
return privateValue;
}
private void setValue(String value)
{
privateValue = value;
}
public Prize()
{
}
/**
Setter method to set all the prize details
@param description
@param color
@param value
*/
public final void SetPrizeDetails(String description, String color, String value)
{
setDescription(description);
setColor(color);
setValue(value);
}
/**
Check if a search term matches the prize
@param searchTerm
@return
*/
public final boolean MatchPrize(String searchTerm)
{
boolean match = false;
// Check if the search matches colour or value and return true if it does
if (searchTerm.equals(getColor()) || searchTerm.equals(getValue()))
{
match = true;
}
return match;
}
/**
Print out the details of the prize
*/
public final void PrintPrize()
{
System.out.println("Description: " + getDescription() + " Colour: " + getColor() + " Value: " + getValue());
}
}
采纳答案by Marco Acierno
When a method don't have a visibility it's package
by default. Read here too.
当一个方法没有可见性时,它是package
默认的。也在这里阅读。
So you should make your
所以你应该让你的
static void main(String[] args)
public.
民众。
public static void main(String[] args)
The reason that the compiler don't complain about the public
is because it doesn't care about the main
method. It doesn't take difference for it if a main exists or not.
编译器不抱怨的原因public
是因为它不关心main
方法。无论是否存在 main 都没有区别。
It's the JVM which need a start point public
and static
to launch your application.
JVM 需要一个起点public
并static
启动您的应用程序。
Try to make other methods named main
and you will see you don't have problems. main
is a name like others.
尝试命名其他方法main
,您会发现没有问题。main
是一个和其他人一样的名字。
回答by mschenk74
The signature for a method to be used as an entry point for a java application is public static void main(String[] args)
.
用作 Java 应用程序入口点的方法的签名是public static void main(String[] args)
.
But if the class isn't meant to be used as entry point, static void main(String[] args)
is also a valid method signature - so the compiler doesn't complain.
但是,如果该类不打算用作入口点,那么static void main(String[] args)
它也是一个有效的方法签名 - 所以编译器不会抱怨。
The compiler doesn't know that you want to use the method as the entry point.
编译器不知道您想使用该方法作为入口点。
回答by Rarw
Your main method must be declared as public so that it can be accessed by the JVM (Java Virtual Machine) that actually executes the Java bytecode. Java bytecodeis what results from compiling the code you write.
您的 main 方法必须声明为公共方法,以便实际执行 Java 字节码的 JVM(Java 虚拟机)可以访问它。Java 字节码是编译您编写的代码的结果。
The JVM is programmed to look for a specific signature for the main method before executing the program. It finds this signature by looking for a specific series of bytes in the bytecode. That signature only results from compiling a method that is - public static void main. If any of the three modifiers (public, static and void) are omitted the bytecode code will look different and the JVM will not be able to find your main method.
JVM 被编程为在执行程序之前查找 main 方法的特定签名。它通过查找字节码中的特定字节序列来找到此签名。该签名仅来自编译一个方法——public static void main。如果省略了三个修饰符(public、static 和 void)中的任何一个,字节码代码看起来会有所不同,JVM 将无法找到您的 main 方法。
You would not find this error when you compile the program because technically there is nothing illegal about having a method called "main." Main is not a reserved wordin Java so there's nothing stopping you from using it as a method (or variable) title. The error will only be detected at runtime when the JVM actually tries to execute the code and cannot find a main method with the appropriate signature.
当您编译程序时,您不会发现此错误,因为从技术上讲,使用名为“main”的方法并没有任何违法之处。Main 不是Java 中的保留字,因此没有什么可以阻止您将其用作方法(或变量)标题。只有在 JVM 实际尝试执行代码并且找不到具有适当签名的 main 方法时,才会在运行时检测到该错误。