Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
线程“main”java.lang.NoSuchMethodError中出现异常:main_Java - Fatal编程技术网

线程“main”java.lang.NoSuchMethodError中出现异常:main

线程“main”java.lang.NoSuchMethodError中出现异常:main,java,Java,下面是我的代码,实际代码中没有错误,但它不会运行,我不知道如何修复它。我已经在这项任务上工作了几个小时,并且在输出框中不断收到相同的错误 import java.util.Scanner; public class whatTheGerbils { public static void main(String[] args) { whatTheGerbils toRun = new whatTheGerbils(); toRun.gerbLab(); } Ge

下面是我的代码,实际代码中没有错误,但它不会运行,我不知道如何修复它。我已经在这项任务上工作了几个小时,并且在输出框中不断收到相同的错误

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

}

您的代码中需要一个main方法才能运行它,请添加如下内容

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

每当Java程序运行时,它都会从一个名为main的方法开始。您得到错误是因为您没有这样的方法。如果您编写这样一个方法,那么它需要做的就是这样

创建whattherbils类的对象。 为创建的对象运行gerbLab方法。 最简单的方法是在whattherbils类中添加以下代码

由于main方法是程序的起点,而您没有,因此错误消息会准确地告诉您。将不会编译,gerbLab不是静态的。
public static void main(String[]  args){
    whatTheGerbils toRun = new whatTheGerbils();
    toRun.gerbLab();
}