如何修复以下错误:"错误:在课程中找不到主要方法"

时间:2014-10-10 01:24:49

标签: java parameters arguments

我基本上是Java新手,为了好玩,我创建了一个股市模拟器/游戏。我知道我的代码不完美"但请记住我是新的,我有1个错误。

  

"错误:在StockPackage.Dog类中找不到主要方法,请定义   主要方法为:public static void main(String [] args)"

我查看了所有可以找到的参数和参数教程,并搜索了StackOverflow的问题,但无法找到解决方案。

如果可以的话,有人可以找到一种方法来删除/修复"错误,或者尽可能地帮助我。在此先感谢!

我的代码:

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);
        }
    }


}

3 个答案:

答案 0 :(得分:7)

您的main方法与通常启动Java应用程序的标准main方法的签名不匹配。你的参数太多了,应该只有一个参数,一个String数组。

所以你必须改变这个:

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

到此:

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

以某种方式获取所需的所有信息,可能来自文件?

答案 1 :(得分:2)

JVM只知道main方法的以下格式,

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

主要关键字已保留,因此您无法将其用于任何其他目的,例如多个参数等。

答案 2 :(得分:2)

您无法更改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...