【问题标题】:Java Exercise: I'm not sure what I'm doing wrong and all help is appreciatedJava 练习:我不确定自己做错了什么,感谢所有帮助
【发布时间】:2020-10-11 19:57:34
【问题描述】:

我正在使用以下代码,并且不断收到此错误消息:错误:在团队团队中找不到主要方法,请将主要方法定义为:public static void main(String[] args)或者 JavaFX 应用程序类必须扩展 javafx.application.Application。欢迎所有建议、意见和更改,因为我完全不知道为什么这不起作用。提前非常感谢,我希望你们有一个美好的一天!

Game.java:

public class Game {

  private Team team1;
  private Team team2;
  private String time;

  public Game(Team t1, Team t2, String time) {
    super();

    this.team1 = team1;
    this.team2 = team2;
    this.time = time;
  }

  public String getTime() {
    return "TIME";
  }

}


Team.java:

public class Team {

  private String name;
  private String sport;
  private String mascot;
  public final static String MOTTO = "Sportsmanship!";

  public Team(String name, String sport, String mascot) {
    this.name = name;
    this.sport = sport;
    this.mascot = mascot;
  }

  //method to set the school name
  public String getName() {
    return name;
  }

  //method to set the sport name
  public String getSport() {
    return sport;
  }

  //method to set the team name
  public String getMascot() {
    return mascot;
  }
}


TestGame.java:

public class TestGame {

  public static void main(String[] args) {
    Team team1 = new Team("Roosevelt High", "Girls Basketball", "Dolphins");
    Team team2 = new Team("Hoover High", "Girls Basketball", "Tigers");
    Game game1 = new Game(team1, team2, "7 PM");

    System.out.println("The game between " + team1.getName() + " " + team1.getSport() +
        " " + team1.getMascot());
    System.out.println("   and " + team2.getName() + " " + team2.getSport() +
        " " + team2.getMascot());
    System.out.println("   takes place at " + game1.getTime());
  }
}


TestTeam.java:

public class TestTeam {

  public static void main(String[] args) {
    Team team1 = new Team("Roosevelt High", "Girls Basketball", "Dolphins");
    Team team2 = new Team("Hoover High", "Boys Wrestling", "Tigers");
    Team team3 = new Team("Lincoln High", "Girls Field Hockey", "Gators");
    display(team1);
    display(team2);
    display(team3);
  }

  public static void display(Team team) {
    System.out.println(
        team.getName() + "" + team.getSport() + "" + team.getMascot() + ""
            + Team.MOTTO);
  }
}

【问题讨论】:

    标签: java class methods


    【解决方案1】:
    1. 您的 Game.java 类不完整,它没有getTime 方法。还有一些大括号被遗漏了。请完成。
    2. 您的 TestGame 类也没有 t1t2g 变量。它有team1team2game。也解决这个问题。
    3. display 方法中有错误。你在这里只传递了一个Team t,但在这个方法中你试图从一些team1team2team3变量中打印数据。它们只是不存在于那个范围内。
    4. 在 display 方法中,您试图获取静态类变量 vie 实例。没有任何意义。

    这是一个工作代码示例。

    Game.java:

    public class Game {
    
      private Team team1;
      private Team team2;
      private String time;
    
      public Game(Team t1, Team t2, String time) {
        super();
    
        this.team1 = team1;
        this.team2 = team2;
        this.time = time;
      }
    
      public String getTime() {
        return "TIME";
      }
    
    }
    

    团队.java:

    public class Team {
    
      private String name;
      private String sport;
      private String mascot;
      public final static String MOTTO = "Sportsmanship!";
    
      public Team(String name, String sport, String mascot) {
        this.name = name;
        this.sport = sport;
        this.mascot = mascot;
      }
    
      //method to set the school name
      public String getName() {
        return name;
      }
    
      //method to set the sport name
      public String getSport() {
        return sport;
      }
    
      //method to set the team name
      public String getMascot() {
        return mascot;
      }
    }
    

    TestGame.java:

    public class TestGame {
    
      public static void main(String[] args) {
        Team team1 = new Team("Roosevelt High", "Girls Basketball", "Dolphins");
        Team team2 = new Team("Hoover High", "Girls Basketball", "Tigers");
        Game game1 = new Game(team1, team2, "7 PM");
    
        System.out.println("The game between " + team1.getName() + " " + team1.getSport() +
            " " + team1.getMascot());
        System.out.println("   and " + team2.getName() + " " + team2.getSport() +
            " " + team2.getMascot());
        System.out.println("   takes place at " + game1.getTime());
      }
    }
    

    TestTeam.java:

    public class TestTeam {
    
      public static void main(String[] args) {
        Team team1 = new Team("Roosevelt High", "Girls Basketball", "Dolphins");
        Team team2 = new Team("Hoover High", "Boys Wrestling", "Tigers");
        Team team3 = new Team("Lincoln High", "Girls Field Hockey", "Gators");
        display(team1);
        display(team2);
        display(team3);
      }
    
      public static void display(Team team) {
        System.out.println(
            team.getName() + "" + team.getSport() + "" + team.getMascot() + ""
                + Team.MOTTO);
      }
    }
    

    【讨论】:

    • 有什么错误?我已经测试了所有这些。效果很好。
    • 错误:在 Team 类中找不到主方法,请将主方法定义为:public static void main(String[] args) 或 JavaFX 应用程序类必须扩展 javafx.application.Application
    • Team 课程不是您的主要课程。您需要使用 public static void main 方法运行类来运行您的应用程序。你有两个:TestTeamTestGame。运行其中任何一个。
    • 我正在运行它们,它们显示正确的输出,但是在 Cengage 上,它告诉我 Tam 类仍未按要求运行。以下是它正在测试的内容。
    • @Test public void unitTest() { Team team = new Team ("Lincoln High", "Girls Field Hockey", "Gators"); assertEquals(team.getName(), "林肯高中"); assertEquals(team.getSport(), "女子曲棍球"); assertEquals(team.getMascot(), “鳄鱼”); Team team2 = new Team("Essex High", "Golf", "Hornets"); assertEquals(team2.getName(), "埃塞克斯高中"); assertEquals(team2.getSport(), "高尔夫"); assertEquals(team2.getMascot(), “黄蜂队”); }
    猜你喜欢
    • 2011-01-27
    • 2021-05-11
    • 1970-01-01
    • 2017-04-21
    • 2018-10-11
    • 2022-07-06
    • 2020-08-25
    • 1970-01-01
    相关资源
    最近更新 更多