有人可以帮助我下面的代码构建没有任何错误,但当我运行它我得到"错误:主方法没有在类程序中找到,请定义主方法为:public static void main(String[] args)"…
知道为什么吗?
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());
}
}
当一个方法没有可见性时,它默认是package
。
所以你应该让你的
static void main(String[] args)
。
public static void main(String[] args)
编译器不抱怨public
的原因是它不关心main
方法。main是否存在对它没有区别
JVM需要一个起始点public
和static
来启动应用程序。
尝试将其他方法命名为main
,您将看到没有问题。main
和其他名字一样
作为java应用程序入口点的方法的签名是public static void main(String[] args)
。
但是如果这个类不打算用作入口点,static void main(String[] args)
也是一个有效的方法签名——所以编译器不会报错。
编译器不知道你想用这个方法作为入口点
必须将main方法声明为public,以便实际执行Java字节码的JVM (Java虚拟机)可以访问它。Java字节码是编译代码后的结果。
JVM被编程为在执行程序之前查找main方法的特定签名。它通过在字节码中查找特定的字节序列来查找此签名。该签名仅在编译一个方法时产生,该方法是公共的。如果省略三个修饰符(public、static和void)中的任何一个,字节码代码将看起来不同,JVM将无法找到您的主方法。
编译程序时不会发现这个错误,因为从技术上讲,拥有一个名为"main"的方法并不违法。Main在Java中不是保留字,因此没有什么可以阻止您将其用作方法(或变量)标题。只有当JVM实际尝试执行代码并且无法找到具有适当签名的主方法时,才会在运行时检测到错误。