Java“java.lang.NoSuchMethodError:main”

时间:2014-12-05 14:16:58

标签: java nosuchmethoderror

根据我对错误的理解,最常见的原因是因为我没有包含" public static void main(String [] args)",但我已经完成了这个在发现错误之前,这让我感到难过。任何人都可以帮助我吗?

import java.io.*;
class basketBall
{
    private String name;    
    private double number;
    private String team;

    // declare getter method public
    public String getName()
    {
        return name;
    }

    // declare setter method public
    public void setName(String n)
    {
        name = n;
    }

    // declare getter method public
    public String getTeam()
    {
        return team;
    }

    // declare setter method public
    public void setTeam(String t)
    {
        team = t;
    }

    // declare getter method public
    public double getNumber()
    {
        return number;
    }

    // declare setter method public
    public void setNumber(double num)
    {
        number = num;
    }

    // declare dribble method
    void dribble()
    {
        System.out.println (name + ", " + number + " dribbles down the court...");
    }

    // declare shoot method
    void shoot()
    {
        System.out.println (name + " shoots... And he scores, for the " + team + "'s!");
    }
}

// test class for basketBall class
class basketBallTester
{
    public static void main (String[] args)
    {
        //construct player and fills in its objects 
        basketBall Player1 = new basketBall();
        // fill in objects of player1
        Player1.setName("Ethan");
    Player1.setTeam("Vikings");
    Player1.setNumber(15);

        // call methods
        Player1.dribble();
        Player1.shoot();
    }
}

1 个答案:

答案 0 :(得分:4)

您可以通过多种方式运行程序。其中之一是:

  1. 创建单独的basketBallTester.java文件,并将您的课程basketBallTester放在那里。
  2. 编译这两个文件:basketBallTester.javabasketBall.java
  3. 使用java basketBallTester
  4. 运行您的程序