【发布时间】:2017-03-05 15:51:44
【问题描述】:
我的第一个名为 Match 的课程创建了一个个人足球/足球比赛。它使您选择2支球队和最终得分。称为“团队”的第二类要高级一些。当调用 play (match match) 方法时,所玩的游戏数增加 1。这部分工作正常。我的 (goalsForThisMatch) 的 else if 语句也可以正常工作。但是,当我检查 Team 类对象时,它应该显示与我在 Match 类中输入的相同的目标和目标。实际发生的情况是,当我在按下 play(Match match) 方法后检查 Team 类时,大多数方法都设置为 0,除了“played”方法(按预期递增 1)以及最终得分如何是。因此,如果我在 match 类中输入了比分,这样主队就进了更多的球并赢得了比赛,那么 Team 类的对象检查器中的 Won 方法将增加 1。我需要其他两个方法在 team 类中与 match 类链接。这些方法是:目标为和目标反对。如果我在 match 类中输入 GoalsFor 为“4”,那么当我检查 Team 类时,goalsFor 也应该设置为 4。
我知道这一切可能听起来很混乱,请原谅我,我太累了,我要睡觉了。希望在早上,有人会为我解决这个问题。
public class Match
// instance variables - replace the example below with your own
private String HomeTeam;
private String AwayTeam;
private int HomeGoals;
private int AwayGoals;
/**
* Constructor for objects of class Match
*/
public Match(String ShortHomeTeamName, String ShortAwayTeamName, int NewHomeGoals, int NewAwayGoals)
{
// initialise instance variables
HomeTeam = ShortHomeTeamName;
AwayTeam = ShortAwayTeamName;
HomeGoals = NewHomeGoals;
AwayGoals = NewAwayGoals;
}
public String getHomeTeamName(){
return HomeTeam;
}
public String getAwayTeamName(){
return AwayTeam;
}
public int getHomeGoals(){
return HomeGoals;
}
public int getAwayGoals(){
return AwayGoals;
}
}
public class Team
private String TeamName;
private String ShortName;
private int Played;
private int GoalsFor;
private int GoalsAgainst;
private int GoalDifference;
private int Won;
private int Drawn;
private int Lost;
private int Points;
/**
* Constructor for objects of class Team
*/
public Team(String FullTeamName, String ShortTeamName)
{
// initialise instance variables
TeamName = FullTeamName;
ShortName = ShortTeamName;
Played = 0;
GoalsFor = 0;
GoalsAgainst = 0;
GoalDifference = 0;
Won = 0;
Drawn = 0;
Lost = 0;
Points = 0;
}
public String getTeamName(){
return TeamName;
}
public String getShortName(){
return ShortName;
}
public int getPlayed(){
return Played;
}
public void getGoalsFor(int InsertGoalsFor){
GoalsFor = InsertGoalsFor;
}
public void getGoalsAgainst(int InsertGoalsAgainst){
GoalsAgainst = InsertGoalsAgainst;
}
public int getGoalDifference(){
return (GoalsFor - GoalsAgainst);
}
public int getWon(){
return Won;
}
public int getDrawn(){
return Drawn;
}
public int getLost(){
return Lost;
}
public int getPoints(){
return Points;
}
public void play(Match match){
Played++;
int GoalsFor = match.getHomeGoals();
int goalsForThisMatch = match.getHomeGoals();
int goalsAgainstThisMatch = match.getAwayGoals();
String homeTeam = match.getHomeTeamName();
String ShortName = match.getHomeTeamName();
if (ShortName.equals(TeamName)){
ShortName = homeTeam;
} else {
ShortName = match.getAwayTeamName();
}
if (goalsForThisMatch > goalsAgainstThisMatch){
Won++;
}
else if (goalsForThisMatch == goalsAgainstThisMatch){
Drawn++;
}
else {
Lost++;
}
}
}
【问题讨论】:
-
代码审查项:您的许多变量名称不遵循Java Naming Conventions,这使您的代码更难阅读。请以小写开头的变量命名,以免它们看起来是类名。