通过方法在不同类之间调用变量

时间:2013-01-29 20:27:43

标签: java arrays variables

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

我已经完成了相当多的任务,但是我在从类中提取单个变量时遇到了一些麻烦。我们刚刚进入类和对象,这是我们关于它们的第一个任务,所以我并不完全理解它们。到目前为止,我已经打印出了这些球队,但是我无法完成个人的胜利,失利,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;
    }
}

谢谢,

1 个答案:

答案 0 :(得分:0)

很抱歉,但我只能理解你问题的一部分,从我的理解,你似乎无法获得个人的胜利,损失,OTL和OTW。我希望这可以回答你的问题,如果没有请澄清一点

  1. 要访问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; 
    
     } 
    
    } 
    
  2. 在HockeyTeam中也将名称声明为静态