Java,制作数组的麻烦

时间:2016-03-29 10:56:20

标签: java

我开始学习Java并且遇到小问题。我有课程Point和abc:

@SuppressWarnings("deprecation")
public void run(String arena){
    for(String key : Handler.playerMap.keySet()){
        if (Handler.playerMap.get(key).contains(arena)){
            Player pt = Bukkit.getPlayer(key);
            String p = pt.getDisplayName();
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getNewScoreboard();
            Objective objective = board.registerNewObjective("test", "dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName(ChatColor.AQUA + "Wave: " );
            Score score = objective.getScore(ChatColor.GREEN + "" + pt.getHealth() + ChatColor.WHITE + p + ChatColor.AQUA);
            int kills = PlayerHandler.kills.get(p);
            score.setScore(kills);
            int pcheck = 0;

但是类abc中的初始化不起作用。当我尝试写第一个数字时,它给出了:

static class Point
    {
        int x;
        int y;
        Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

static class abc
    {
        abc() 
        {
            Scanner s = new Scanner(System.in);
            Point[] p = new Point[2];
            for (int i = 0; i < 2; ++i) {
                p[i].x = s.nextInt();
                p[i].y = s.nextInt();
            }
        }
    }

我该怎么做才能让它发挥作用?

1 个答案:

答案 0 :(得分:3)

您只创建了Points数组,而不是数组中的实际Points。

更改

p[i].x = s.nextInt();
p[i].y = s.nextInt();

p[i] = new Point(s.nextInt(), s.nextInt());

int x = s.nextInt();
int y = s.nextInt();
p[i] = new Point(x, y);