我正在为计算机科学课做作业。

我已经完成了很多分配工作,但是在从类中提取单个变量时遇到了一些麻烦。我们只是进入类和对象,这是我们对它们的第一次分配,因此我并不完全理解所有这些。到目前为止,我已经能够打印出各个团队,但是我还无法提取出个人的胜利,失败,OTL和OTW,以便我可以计算出每个单独的团队是否都是获胜的团队。

到目前为止,我所做的是创建一个名为winningRecord和getPoints的类,该类返回一个布尔值,该布尔值决定是否是获胜的团队。 (一支获胜球队的公式是,如果得分是>玩过的游戏* 1.5(因为这是偶数记录)。

我不知道如何获取统计信息,因为它必须在HockeyTeam类中编写。我已经进行了设置,以便构造函数公开设置变量,以便可以访问,但是就访问它们而言,我感到很困惑。

至于一旦我能够访问它们就将它们存储起来,我是否会制作一个并行方法,该方法具有每个团队的分数,而每个仓位仅分配一个数字?

这是所有代码,感谢您的关注。

public class A1Q2fixed {
    public static void main(String[] parms) { // main method
        processHockeyTeams();
    }

    /*****************************/

    public static void processHockeyTeams() { // processing method
        boolean[] winningRecord;
        HockeyTeam[] hockeyTeams;

        hockeyTeams = createTeams();
        printTeams(hockeyTeams);
        System.out.print("*********************\n");
        printWinningTeams();
        winningRecord = HockeyTeam.winningRecord(hockeyTeams);
        // printWinningTeams(hockeyTeams);
    }

    /*********************************/

    public static void printTeams(HockeyTeam[] hockeyTeams) {
        for (int i = 0; i < hockeyTeams.length; i++) {
            System.out.println(hockeyTeams[i]);
        }

    }

    public static void printWinningTeams() {

    }

    public static HockeyTeam[] createTeams() {
        HockeyTeam[] teams;
        HockeyTeam team;
        int count;

        teams = new HockeyTeam[HockeyTeams.getNumberTeams()];
        team = HockeyTeams.getTeam();
        for (count = 0; (count < teams.length) && (team != null); count++) {
            teams[count] = team;
            team = HockeyTeams.getTeam();
        }
        return teams;
    }
}

/* hockey team class *******/

class HockeyTeam {
    public String name;
    public int wins;
    public int otw;
    public int otl;
    public int losses;

    public HockeyTeam(String name, int wins, int otw, int otl, int losses) {
        this.name = name;
        this.wins = wins;
        this.otw = otw;
        this.otl = otl;
        this.losses = losses;
    }

    public String toString() {
        System.out.println(name);

        return " W:" + wins + " OTW:" + otw + " OTL:" + otl + " L:" + losses;
    }

    public static boolean[] winningRecord(HockeyTeam[] hockeyTeam) {
        boolean array[] = new boolean[hockeyTeam.length];

        String name;
        int wins;
        int otw;
        int otl;
        int losses;

        for (int i = 0; i < hockeyTeam.length; i++) {
            System.out.println(HockeyTeam.name);
        }
        return array;
    }

    public static int getPoints() {
        int points = 0;
        return points;
    }
}

/* hockey teams class *******************/
class HockeyTeams {
    private static int count = 0;

    private static HockeyTeam[] hockeyTeams = {
            new HockeyTeam("Canada", 5, 3, 0, 0),
            new HockeyTeam("Russia", 5, 1, 1, 2),
            new HockeyTeam("Finland", 3, 2, 1, 3),
            new HockeyTeam("Sweden", 4, 1, 1, 4),
            new HockeyTeam("USA", 1, 2, 2, 3), };

    public static int getNumberTeams() {
        return hockeyTeams.length;
    }

    public static HockeyTeam getTeam() {
        HockeyTeam hockeyTeam;

        hockeyTeam = null;
        if (count < hockeyTeams.length) {
            hockeyTeam = hockeyTeams[count];
            count++;
        }
        return hockeyTeam;
    }
}


谢谢,

马特

最佳答案

抱歉,但根据我的理解,我似乎只能理解您的部分问题,您似乎无法获得个人的赢,输,OTL和OTW。我希望这能回答您的问题,如果不能,请澄清一下


要访问OTL,OTW,请执行以下循环:

public class A1Q2fixed

{
 public static void main(String[] parms) // main method
 {
  processHockeyTeams();
 }
 /*****************************/

 public static void processHockeyTeams()  // processing method
 {

  boolean[] winningRecord;

  HockeyTeam[] hockeyTeams;



  hockeyTeams = createTeams();

  printTeams(hockeyTeams);

  System.out.print("*********************\n");

  printWinningTeams();

    winningRecord = HockeyTeam.winningRecord(hockeyTeams);

    for(HockeyTeam h:hockeyTeams)
    {
        System.out.println(h.losses);//To access and print losses

             System.out.println(h.otw);//To access and print otw

             System.out.println(h.otl);//To access and print otl

    }


  // printWinningTeams(hockeyTeams);
 }

 /*********************************/

 public static void printTeams(HockeyTeam[] hockeyTeams)

 {

   for (int i = 0; i < hockeyTeams.length; i++)

   {

     System.out.println(hockeyTeams[i]);

   }

 }

 public static void printWinningTeams()

 {

 }

 public static HockeyTeam[] createTeams()
 {
  HockeyTeam[] teams;

  HockeyTeam team;

  int count;

  teams = new HockeyTeam[HockeyTeams.getNumberTeams()];

  team = HockeyTeams.getTeam();

  for (count=0; (count<teams.length) && (team!=null); count++)
  {

   teams[count] = team;

   team = HockeyTeams.getTeam();

  }

  return teams;

 }

}

同时在HockeyTeam中将名称声明为Static

09-05 19:11