Java练习:我不确定我做错了什么,非常感谢所有的帮助。

huangapple go评论68阅读模式
英文:

Java Exercise: I'm not sure what I'm doing wrong and all help is appreciated

问题

我正在处理以下代码片段并且一直收到以下错误信息*错误在类Team中找不到main方法请将主方法定义为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);
  }
}
英文:

I am working with the following pieces of code and I keep getting this error message: Error: Main method not found in class Team, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application. All suggestions, opinions, and changes are welcome as I am completely lost on why this isn't working. Thanks so much in advance and I hope you all have a great day!

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

答案1

得分: 0

  1. 你的Game.java类是不完整的,它没有getTime方法。此外,一些大括号也缺失了。请补充完整。
public class Game {
    
  private Team team1;
  private Team team2;
  private String time;
  
  public Game(Team t1, Team t2, String time) {
    super();
    
    this.team1 = t1;
    this.team2 = t2;
    this.time = time;
  }
  
  public String getTime() {
    return "时间";
  }
  
}
  1. 同样,你的TestGame类中的变量名不是t1t2g,而是team1team2game。请进行修正。
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("比赛是在" + team1.getName() + " " + team1.getSport() +
        " " + team1.getMascot());
    System.out.println("   与" + team2.getName() + " " + team2.getSport() +
        " " + team2.getMascot());
    System.out.println("   进行于" + game1.getTime());
  }
}
  1. 你的display方法中存在错误。你在这里只传递了一个Team t,但在这个方法内部,你尝试打印来自一些team1team2team3变量的数据。它们在这个范围内并不存在。
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);
  }
}
  1. display方法内部,你试图通过实例来获取静态类变量。这没有意义。
public static final String MOTTO = "Sportsmanship!";

这是一个可工作的代码示例。

英文:
  1. Your Game.java class is incomplete, it doesn't have getTime method. Also there are missed some curly brackets. Complete it please.
  2. Also your TestGame class has not t1, t2 and g variables. It has team1, team2 and game instead. Fix this too.
  3. You have an error inside your display method. You pass here only one Team t, but inside this method you're trying to print data from some team1, team2, team3 variables. They just doesn't exists in that scope.
  4. Inside the display method you're trying to get static class variable vie instance. There is no sence in that.

Here is an example of working code.

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

huangapple
  • 本文由 发表于 2020年10月12日 03:57:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64308435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定