简体   繁体   English

如何解决以下错误:“错误:在类中找不到主要方法”

[英]How to fix the following error: “Error: Main method not found in class”

I'm essentially new to Java and for fun I'm creating a Stock Market Simulator/Game. 我本质上不是Java新手,为了娱乐,我正在创建一个Stock Simulator / Game。 I know my code isn't "perfect" but remember I'm new and i have 1 error. 我知道我的代码不是“完美的”,但请记住我是新手,并且有1个错误。

"Error: Main method not found in class StockPackage.Dog, please define the main method as: public static void main(String[] args)" “错误:在类StockPackage.Dog中找不到主要方法,请将主要方法定义为:public static void main(String [] args)”

I've looked at all the argument and parameter tutorials I could find and searched StackOverflow's Questions and couldn't find a solution. 我看了所有可以找到的参数和参数教程,并搜索了StackOverflow的Questions,但是找不到解决方案。

If you can, can someone come up with a way to "Delete/Fix" the error, or just help me out as much as you can. 如果可以的话,有人可以提出一种“删除/修复”错误的方法,还是尽可能地帮助我。 Thanks in Advance! 提前致谢!

My Code: 我的代码:

package StockPackage;

import java.util.Scanner;    

public class Dog {      

    public static void main(String[] args, int CurMoney, String[] inputStocks, 
        int[] StockMoney) {
        Dog Run = new Dog();
        Run.MainScript();
        Run.OptionStartingMethod(inputStocks, CurMoney, StockMoney);
    }     

//==============================================================================
//                          ||Objects||
    void options(String[] inputStocks, int CurMoney, int[] StockMoney) {
        String optionEXIT, optionStartStock, optionBuyStock, optionSellStock;
        String optionCheat, optionInfo, optionCredits, optionClose, OptionInput;
        int i, j, k;            

        Scanner optionInput = new Scanner(System.in);

        Dog FlixSibling = new Dog();            

        System.out.println("=============| Options | =============");
        System.out.println(" optionEXIT       " + " optionStartStock   " + "optionBuyStock");
        System.out.println(" optionSellStock  " + " optionCheat   " + "     OptionInfo  ");
        System.out.println(" optionCredits    " + " optionClose   " + "        optionCheckCash");
        System.out.println();
        System.out.print("==|  ");

        OptionInput = optionInput.nextLine();

        if(OptionInput.equals("optionEXIT")) {
            FlixSibling.Close();
        } else if(OptionInput.equals("optionStartStock")) {

            StartStock(inputStocks, StockMoney, CurMoney);
        } else if(OptionInput.equals("optionBuyStock")) {

        } else if(OptionInput.equals("optionSellStock")) {

        } else if(OptionInput.equals("optionCheat")) {

        } else if(OptionInput.equals("optionInfo")) {

        } else if(OptionInput.equals("optionCredits")) {

        } else if(OptionInput.equals("optionClose")) {
            optionClose1(inputStocks, CurMoney, StockMoney);
        }

    }
//==============================================================================
//                        ||Option Holders||
    void optionClose1(String[] inputStocks, int CurMoney, int[] StockMoney) {
        int i, j, k;
        for(i = 0; i <= 25; i++) {
            System.out.println();
        }
        MainScript();
        for(i = 0; i <= 3; i++) {
            System.out.println();
        }
    }
    void OptionStartingMethod(String[] inputStocks, int CurMoney, int[] StockMoney) {
        Scanner InputBoard1 = new Scanner(System.in);

        String Options;
        System.out.println();
        System.out.println("Option: Press 'P'");

        Options = InputBoard1.nextLine();

        if(Options.equals("P")) {
            options(inputStocks, CurMoney, StockMoney);
        }
    }
//==============================================================================
//                      ||StartStock Method||
    void StartStock(String[] inputStocks, int[] StockMoney, int CurMoney) {
        Dog StartStocking = new Dog();

        String Answer;
        String name, price;
        int CurCash;

        Scanner StartingStockInput = new Scanner(System.in);

        System.out.println();
        System.out.println("Do you Wish to create a Stock? (y/n)");
        Answer = StartingStockInput.nextLine();

        if(Answer.equals("y")) {
            System.out.println("$15000 Has been taken out of your Account");
            CurMoney -= 15000;    // for Creating the Stock

            System.out.println("Name|  ");
            name = StartingStockInput.nextLine();

            System.out.println("input Amount of Cash|  ");
            CurCash = StartingStockInput.nextInt();
            if(CurCash > CurMoney) {
                CurCash = CurMoney;
            }
            MainScript();


        } else if(Answer.equals("n")) {
            optionClose1(inputStocks, CurMoney, StockMoney);
        }

    }


//==============================================================================
//                        ||Close Method||
    void Close() {
        System.out.println();
        System.out.println();
        System.out.println("You Exit'ed out of Stock market SIM");
    }

//==============================================================================
//                        ||Main Method||
    void MainScript() {

        System.out.println("Welcome to the Stock Market");

                //        ||Variables||
        int StockPercent;
        int CurMoney;
        String Options;
        CurMoney = 64000;
        String Place, Place1, Place2;
        Place = "0";
        Place1 = ",000";
        Place2 = ",000,000";
        String nothing = "|Under Construction|";
        int i, j, k;

        Dog Flix = new Dog();

        boolean Construction = false;

        int[] StockMoney;
        StockMoney = new int[500];

        String[] inputStocks;
        inputStocks = new String[200]; // Used for storing the inputed Stocks
                                       // 50 And Above are used for Creating Stocks
        String per = "%";

        inputStocks[0] = "Google"; 
        StockMoney[50] = 0; 
        StockMoney[0] = 400000000;     // Using the 2 bit 000's for the millions place;

        inputStocks[2] = "MicroSoft";
        StockMoney[51] = 0;
        StockMoney[1] = 250000000;

        inputStocks[4] = "Yahoo";
        StockMoney[52] = 0;
        StockMoney[2] = 100000000;

        inputStocks[6] = "Cheeez It's";
        StockMoney[53] = 0; 
        StockMoney[3] = 50000;




                //         ||input From User||
        Scanner InputBoard = new Scanner(System.in);



                //         ||Output From Program||

        System.out.println("[]---------------| Stocks |---------------[]");
        System.out.println(inputStocks[0] + "      " + StockMoney[0] + "    " + StockMoney[50] + per);
        System.out.println(inputStocks[2] + "   " + StockMoney[1] + "    " + StockMoney[51] + per);
        System.out.println(inputStocks[4] + "       " + StockMoney[2] + "    " + StockMoney[52] + per);
        System.out.println(inputStocks[6] + " " + StockMoney[3] + "        " + StockMoney[53] + per);
        System.out.println();
        System.out.println();



        if(Construction) {
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
            System.out.println(nothing);
        }
    }


}

Your main method does not match the signature of a standard main method, that normally starts a Java application. 您的main方法与通常启动Java应用程序的标准main方法的签名不匹配。 Yours has way too many parameters and should have only one parameter, an array of String. 您的参数太多了,应该只有一个参数,即String数组。

So you must change this: 因此,您必须更改此设置:

// incorrect main method signature
public static void main(String[] args, int CurMoney, String[] inputStocks, int[] StockMoney) {

to this: 对此:

// correct main method signature
public static void main(String[] args) {

and get all the information needed in some way, perhaps from a file? 并以某种方式(例如从文件中)获取所需的所有信息?

JVM only knows the following format of main method, JVM只知道以下main方法格式,

public static void main(String[] args)
{
}

the main keyword is reserved so you can't use it for any other purpose like multiple parameters and so on. main关键字是保留关键字,因此您不能将其用于其他目的(例如多个参数)。

You can't change the public static void main method so you have tu put in correct way 您无法更改public static void main方法,因此您必须以正确的方式放置

    public static void main(String[] args) {

        int CurMoney;//fill the varible
        String[] inputStocks;//fill the Array
        int[] StockMoney;//fill the Array
        Dog Run = new Dog();
        Run.MainScript();
        Run.OptionStartingMethod(inputStocks, CurMoney, StockMoney);
    }

rest of the code you have...   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM