公共类中的一个类,并创建一个新对象访问不同的变量

时间:2018-01-10 17:43:01

标签: java

我未能找到如何在公共课堂上上课。没有语法错误。错误来自接收类的方法。

我正在使用Java博士来运行它。我尝试在其他用户解决方案中建议单独的文件附件,但我的版本没有其他用户版本中的工具。请告诉我如何实现这一目标。

public class TestLab2Bronze {
  public void main(String[] args) {
    //Create the teams in the Central division of the NHL.
    //This is intentionally very simple brute-force code.
    HockeyTeam team1 = new HockeyTeam("Winnipeg",22,14,8,"=",0);
    HockeyTeam team2 = new HockeyTeam("Chicago",28,13,2,"=",0);
    HockeyTeam team3 = new HockeyTeam("Colorado",18,17,8,"=",0);
    HockeyTeam team4 = new HockeyTeam("St. Louis",27,13,3,"=",0);
    HockeyTeam team5 = new HockeyTeam("Dallas",19,16,7,"=",0);
    HockeyTeam team6 = new HockeyTeam("Minnesota",18,19,5,"=",0);
    HockeyTeam team7 = new HockeyTeam("Nashville",29,9,4,"=",0);

    //Print out all 7 objects, again with simple code.
    System.out.println("Initial teams:\n" +
              team1 + "\n" + team2 + "\n" + team3 + "\n" + team4 + "\n" +
              team5 + "\n" + team6 + "\n" + team7 + "\n");

    //Record the results of some fictional games
    team1.won(); team2.lost();          //Winnipeg beat Chicago
    team1.won(); team3.lostOvertime();  //Winnipeg beat Colorado
    team1.won(); team4.lostOvertime();  //Winnipeg beat St. Louis
    team1.won(); team5.lost();          //Winnipeg beat Dallas
    team1.won(); team6.lost();          //Winnipeg beat Minnesota
    team1.won(); team7.lostOvertime();  //Winnipeg beat Nashville
    //OK. So the Jets are really hot right now.
    team4.won(); team2.lost();          //St. Louis beat Chicago
    team3.won(); team5.lost();          //Colorado beat Dallas   

    //Print out the 7 objects again, to see the changes.
    System.out.println("Final teams:\n" +
              team1 + "\n" + team2 + "\n" + team3 + "\n" + team4 + "\n" +
              team5 + "\n" + team6 + "\n" + team7 + "\n");
  }
}

class HockeyTeam {
  public int point;

  public HockeyTeam(String city, int scoreone, int scoretwo, int scorethree, String equals, int point) {
    this.point = point;
  }

  public void won() {
    point += 2;
  }

  public void lost() {
    point += 0;
  }

  public void lostOvertime() {
    point += 1;
  }

  public int points() {
    return point;
  }
}

3 个答案:

答案 0 :(得分:0)

检查课程的位置。如果您使用8n相同的pkg,则没有问题。如果存在不同的classess路径,它将发出。那时你需要设置classpath od调用类。请转换文件的类路径,然后将其与当前类

一起使用

答案 1 :(得分:0)

main方法必须是静态的:

  

public static void main(String [] args)

HockeyTeam应该在Test5Lab2Bronze类中,静态并声明如下:

  

公共静态课程HockeyTeam

代码应如下所示:

public class TestLab2Bronze {

    public static void main(String[] args) {
        // Create the teams in the Central division of the NHL.
        // This is intentionally very simple brute-force code.
        HockeyTeam team1 = new HockeyTeam("Winnipeg", 22, 14, 8, "=", 0);
        HockeyTeam team2 = new HockeyTeam("Chicago", 28, 13, 2, "=", 0);
        HockeyTeam team3 = new HockeyTeam("Colorado", 18, 17, 8, "=", 0);
        HockeyTeam team4 = new HockeyTeam("St. Louis", 27, 13, 3, "=", 0);
        HockeyTeam team5 = new HockeyTeam("Dallas", 19, 16, 7, "=", 0);
        HockeyTeam team6 = new HockeyTeam("Minnesota", 18, 19, 5, "=", 0);
        HockeyTeam team7 = new HockeyTeam("Nashville", 29, 9, 4, "=", 0);

        // Print out all 7 objects, again with simple code.
        System.out.println("Initial teams:\n" + team1 + "\n" + team2 + "\n"
                + team3 + "\n" + team4 + "\n" + team5 + "\n" + team6 + "\n"
                + team7 + "\n");

        // Record the results of some fictional games
        team1.won();
        team2.lost(); // Winnipeg beat Chicago
        team1.won();
        team3.lostOvertime(); // Winnipeg beat Colorado
        team1.won();
        team4.lostOvertime(); // Winnipeg beat St. Louis
        team1.won();
        team5.lost(); // Winnipeg beat Dallas
        team1.won();
        team6.lost(); // Winnipeg beat Minnesota
        team1.won();
        team7.lostOvertime(); // Winnipeg beat Nashville
        // OK. So the Jets are really hot right now.
        team4.won();
        team2.lost(); // St. Louis beat Chicago
        team3.won();
        team5.lost(); // Colorado beat Dallas

        // Print out the 7 objects again, to see the changes.
        System.out.println("Final teams:\n" + team1 + "\n" + team2 + "\n"
                + team3 + "\n" + team4 + "\n" + team5 + "\n" + team6 + "\n"
                + team7 + "\n");

    }// main

    public static class HockeyTeam {
        public int point;

        public HockeyTeam(String city, int scoreone, int scoretwo,
                int scorethree, String equals, int point)

        {
            this.point = point;

        }

        public void won()

        {
            point += 2;

        }

        public void lost()

        {
            point += 0;

        }

        public void lostOvertime()

        {
            point += 1;

        }

        public int points()

        {
            return point;

        }

    }

}

答案 2 :(得分:0)

是的我已经更改了Hockey类位置并将其设置为静态并运行查找,但它不会以文本语言形式打印出值(可读)。

欢迎来到DrJava。工作目录是C:\ Users \ Acer

  

运行TestLab2Bronze   初始团队:   TestLab2Bronze $ @ HockeyTeam 67dd9e   TestLab2Bronze $ @ HockeyTeam 143b388   TestLab2Bronze $ @ HockeyTeam 1f2e781   TestLab2Bronze $ @ HockeyTeam 14b9a40   TestLab2Bronze $ @ HockeyTeam 1842d5a   TestLab2Bronze $ @ HockeyTeam 1bd1811   TestLab2Bronze $ HockeyTeam @ 1ed3c88

决赛队伍: TestLab2Bronze $ @ HockeyTeam 67dd9e TestLab2Bronze $ @ HockeyTeam 143b388 TestLab2Bronze $ @ HockeyTeam 1f2e781 TestLab2Bronze $ @ HockeyTeam 14b9a40 TestLab2Bronze $ @ HockeyTeam 1842d5a TestLab2Bronze $ @ HockeyTeam 1bd1811 TestLab2Bronze $ HockeyTeam @ 1ed3c88

我想打印:

初始球队:温尼伯(22,14,8 = 52)芝加哥(28,13,2 = 58)科罗拉多(18,17,8 = 44)圣路易斯(27,13,3 = 57)达拉斯( 19,16,7 = 45)明尼苏达(18,19,5 = 41)纳什维尔(29,9,4 = 62)

最终球队:温尼伯(28,14,8 = 64)芝加哥(28,15,2 = 58)科罗拉多(19,17,9 = 47)圣路易斯(28,13,4 = 60)达拉斯( 19,18,7 = 45)明尼苏达州(18,20,5 = 41)纳什维尔(29,9,5 = 63)`

与原始文本相同的间距只是可读的文本。感谢您的建议和解决方案