有人可以帮助我使用下面的代码构建没有任何错误但是当我运行它我得到"错误:在课程程序中找不到主要方法,请将主要方法定义为: 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());
}
}
答案 0 :(得分:8)
如果某个方法默认情况下不会显示package
。 Read here too.
所以你应该做你的
static void main(String[] args)
公共
public static void main(String[] args)
编译器不抱怨public
的原因是因为它并不关心main
方法。如果主要存在与否,它并没有区别。
JVM需要一个起点public
和static
才能启动您的应用程序。
尝试制作名为main
的其他方法,您会发现自己没有问题。 main
就像其他人一样。
答案 1 :(得分:1)
用作Java应用程序入口点的方法的签名是public static void main(String[] args)
。
但如果该类不打算用作入口点,static void main(String[] args)
也是一个有效的方法签名 - 所以编译器不会抱怨。
编译器不知道您要将该方法用作入口点。
答案 2 :(得分:0)
必须将main方法声明为public,以便实际执行Java字节码的JVM(Java Virtual Machine)可以访问它。 Java bytecode是编写您编写的代码的结果。
JVM被编程为在执行程序之前查找main方法的特定签名。它通过在字节码中查找特定的字节序列来找到此签名。该签名仅来自编译一个方法 - public static void main。如果省略三个修饰符(public,static和void)中的任何一个,则字节码代码将看起来不同,并且JVM将无法找到您的main方法。
编译程序时不会发现此错误,因为从技术上讲,使用名为&#34; main的方法并不违法。&#34; Main不是Java中的reserved word所以没有什么能阻止你将它用作方法(或变量)标题。只有在JVM实际尝试执行代码并且找不到具有适当签名的main方法时,才会在运行时检测到错误。