簡體   English   中英

線程“main”中的異常 java.lang.NoSuchMethodError: main

[英]Exception in thread "main" java.lang.NoSuchMethodError: main

下面是我的代碼,實際代碼中沒有錯誤,但它不會運行,我不知道如何修復它。 我已經在這個作業上工作了幾個小時,並一遍又一遍地在輸出框中收到相同的錯誤。

import java.util.Scanner;

public class whatTheGerbils 
{
   public static void main(String[] args)
{
    whatTheGerbils toRun = new whatTheGerbils();
    toRun.gerbLab();
}

    Gerbil[] gerbilArray;
    Food[] foodArray;
    int numGerbils;
    int foodnum;

    public whatTheGerbils() {}




    public void gerbLab()
    {

        boolean gerbLab = true;
        while(gerbLab)
        {
            foodnum = 0;
            numGerbils = 0;
            String nameOfFood = "";
            String colorOfFood;
            int maxAmount = 0;
            String ID;
            String name;
            String onebite;
            String oneescape;
            boolean bite = true;
            boolean escape = true;
            Scanner keyboard = new Scanner(System.in);
            System.out.println("How many types of food do the gerbils eat?");
            int foodnum = Integer.parseInt(keyboard.nextLine());
            foodArray = new Food[foodnum];
            for (int i = 0; i < foodnum; i++)
            {
                System.out.println("The name of food item " + (i+1) + ":"); //this is different
                nameOfFood = keyboard.nextLine();

                System.out.println("The color of food item " + (i+1) + ":");
                colorOfFood = keyboard.nextLine();

                System.out.println("Maximum amount consumed per gerbil:");
                maxAmount = keyboard.nextInt();
                keyboard.nextLine();

                foodArray[i] = new Food(nameOfFood, colorOfFood, maxAmount);
            }

            System.out.println("How many gerbils are in the lab?");
            int numGerbils = Integer.parseInt(keyboard.nextLine()); // this is different
            gerbilArray = new Gerbil[numGerbils];

            for (int i = 0; i < numGerbils; i++)
            {
                System.out.println("Gerbil " + (i+1) + "'s lab ID:");
                ID = keyboard.nextLine();

                System.out.println("What name did the undergrads give to " 
                        + ID + "?");
                name = keyboard.nextLine();

                Food[] gerbsBetterThanMice = new Food[foodnum];
                int foodType = 0;
                for(int k = 0; k < foodnum; k++)
                {
                    boolean unsound = true;
                    while(unsound)
                    {
                        System.out.println("How much " + foodArray[k].getnameOfFood() + "does" + name + " eat?");
                        foodType = keyboard.nextInt();
                        keyboard.nextLine();

                        if(foodType <= foodArray[k].getamtOfFood())
                        {
                            unsound = false;
                        }
                        else
                        { 
                            System.out.println("try again");
                        }
                    }

                    gerbsBetterThanMice[k] = new Food(foodArray[k].getnameOfFood(), foodArray[k].getcolorOfFood(), foodType);
                }

                boolean runIt = true;
                while (runIt)
                {
                    System.out.println("Does " + ID + "bite? (Type in True or False)");
                    onebite = keyboard.nextLine();

                    if(onebite.equalsIgnoreCase("True"))
                    {
                        bite = true;
                        runIt = false;
                    }
                    else if (onebite.equalsIgnoreCase("False"))
                    {
                        bite = false;
                        runIt = false;
                    }
                    else {
                        System.out.println("try again");
                    }
                }

                runIt = true;
                while(runIt)
                {
                    System.out.println("Does " + ID + "try to escape? (Type in True or False)");
                    oneescape = keyboard.nextLine();

                    if(oneescape.equalsIgnoreCase("True"))
                    {
                        escape = true;
                        runIt = false;
                    }
                    else if(oneescape.equalsIgnoreCase("False"))
                    {
                        escape = false;
                        runIt = false;
                    }
                    else
                    {
                        System.out.println("try again");
                    }
                }

                gerbilArray[i] = new Gerbil(ID, name, bite, escape, gerbsBetterThanMice);
            }

            for(int i = 0; i < numGerbils; i++)
            {
                for(int k = 0; k < numGerbils; k++)
                {
                    if(gerbilArray[i].getID().compareTo(gerbilArray[k].getID()) > 
                    0)
                    {
                        Gerbil tar = gerbilArray[i];
                        gerbilArray[i] = gerbilArray[k];
                        gerbilArray[k] = tar;
                    }
                }
            }

            boolean stop = false;
            String prompt = "";
            while(!stop)
            {
                System.out.println("Which function would you like to carry out: average, search, restart, or quit?");
                prompt = keyboard.nextLine();

                if (prompt.equalsIgnoreCase("average"))
                {
                    System.out.println(averageFood());
                }
                else if (prompt.equalsIgnoreCase("search"))
                {
                    String searchForID = "";
                    System.out.println("What lab ID do you want to search for?");
                    searchForID = keyboard.nextLine();

                    Gerbil findGerbil = findThatRat(searchForID);

                    if(findGerbil != null)
                    {
                        int integer1 = 0;
                        int integer2 = 0;
                        for (int i = 0; i < numGerbils; i++)
                        {
                            integer1 = integer1 + foodArray[i].getamtOfFood();
                            integer2 = integer2 + findGerbil.getGerbFood()[i].getamtOfFood();
                        }
                        System.out.println("Gerbil name: " + 
                                findGerbil.getName() + " (" + findGerbil.getBite() + ", " + 
                                findGerbil.getEscape() + ") " + 
                                Integer.toString(integer2) + "/" + Integer.toString(integer1));
                    }
                    else
                    { 
                        System.out.println("error");
                    }
                }

                else if (prompt.equalsIgnoreCase("restart"))
                {
                    stop = true;
                    break;
                }

                else if(prompt.equalsIgnoreCase("quit"))
                {
                    System.out.println("program is going to quit");
                    gerbLab = false;
                    stop = true;
                    keyboard.close();
                }
                else
                { 
                    System.out.println("error, you did not type average, search, restart or quit");
                }
            }
        }
    }


    public String averageFood()
    {
        String averageStuff = "";
        double avg = 0;
        double num1 = 0;
        double num2 = 0;
        for (int i = 0; i < numGerbils; i++)
        {
            averageStuff = averageStuff + gerbilArray[i].getID();
            averageStuff = averageStuff + " (";
            averageStuff = averageStuff + gerbilArray[i].getName();
            averageStuff = averageStuff + ") ";
            for (int k = 0; k < foodnum; k++)
            {
                num1 = num1 + foodArray[k].getamtOfFood();
                num2 = num2 + gerbilArray[i].getGerbFood()[k].getamtOfFood();
            }
            avg = 100*(num2/num1);
            avg = Math.round(avg);
            averageStuff = averageStuff + Double.toString(avg);
            averageStuff = averageStuff + "%\n";
        }
        return averageStuff;
    }

    public Gerbil findThatRat (String ID)
    {
        for(int i = 0; i < numGerbils; i++)
        {
            if(ID.contentEquals(gerbilArray[i].getID()))
            {
                return gerbilArray[i];
            }
        }
        return null;
    }

}

每當 Java 程序運行時,它都會從一個名為main的方法開始。 您收到錯誤是因為您沒有這樣的方法。 如果你寫了這樣一個方法,那么它需要做的就是這個。

  • 創建whatTheGerbils類的對象。
  • 為創建的對象運行gerbLab()方法。

最簡單的方法是在whatTheGerbils類中添加以下代碼。

public static void main(String[]  args){
    whatTheGerbils toRun = new whatTheGerbils();
    toRun.gerbLab();
}

您的代碼中需要一個 main 方法才能運行,添加如下內容

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM