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

时间:2014-04-19 21:45:39

标签: java

下面是我的代码,实际代码中没有错误,但它不会运行,我不知道如何修复它。我一直在处理这个任务几个小时,并一直在输出框中一遍又一遍地收到同样的错误。

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

}

2 个答案:

答案 0 :(得分:1)

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

  • 创建whatTheGerbils类的对象。
  • 为已创建的对象运行gerbLab()方法。

最简单的方法是在whatTheGerbils类中添加以下代码。

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

答案 1 :(得分:-1)

您需要在代码中使用main方法才能运行,添加类似

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