0

我看不出我在这里做错了什么,或者如果没有这样声明,这里的任何东西都是静态的。我只需要在这里指出正确的方向。

测试代码:

public class PaintballPlayerTest
{
//Test program for PaintballPlayer assignment

public static void main (String [] args)
{
//Part 1 check constructor & toString --(make sure ID is working too)
PaintballPlayer sheldon = new PaintballPlayer ("Sheldon", "Lee", "Cooper");
PaintballPlayer leonard = new PaintballPlayer ("Leonard", "Hofstadter");
PaintballPlayer amy = new PaintballPlayer ("Amy", "Farrah", "Fowler");

System.out.println(sheldon);
System.out.println(leonard);

//Part 2 test getTotalPlayer --should be 3
System.out.println("The team has this many players " + PaintballPlayer.getTotalPlayers());

我的代码:

import java.util.*;

public class PaintballPlayer
{
private String firstName, middleName, lastName;
private String secFirst, secLast;
private int id;
private int count;
private static int totalPlayers;
private int playerID;
private int players;

public PaintballPlayer(String first, String middle, String last)
{
    count = 0;
    id = totalPlayers++;
    players = count++;
    firstName = first;
    middleName = middle;
    lastName = last;
}

public PaintballPlayer(String f, String l)
{
    this (f,"",l);
    id = count++;


}


 public PaintballPlayer() 
{
totalPlayers++;
 }

 public static int getTotalPlayers() 
{
return totalPlayers;
}

public String toString()
{
    String name;
    String n;
    name = firstName + " " + middleName + " " + lastName;

    return name;

}


public int getPlayerID()
{
    playerID = count;
    return playerID;
}
  }

同样,我的问题在于 getTotalPlayers() 方法。

编辑:这是我编辑的代码应用提供的修复。谢谢!

4

3 回答 3

3

getTotalPlayers()不是静态方法,所以你需要一个实例PaintballPlayer来调用这个方法。

如果你想在里面存储玩家总数PaintballPlayer,你需要一个静态属性(所有实例的引用相同):

class PaintballPlayer {

  private static int totalPlayers;

  public PaintballPlayer() {
    totalPlayers++;
  }

  public static int getTotalPlayers() {
    return totalPlayers;
  }
}
于 2013-04-22T17:31:15.340 回答
0

在您的代码上,在构造函数 #1 上存在一些问题

public PaintballPlayer(String first, String middle, String last)
{
    count = 0;
    players = 0;
    id = count++;
    players = count++;
    firstName = first;
    middleName = middle;
    lastName = last;
}

变量 player 和 count 被重置,而变量 count 增加了两倍。

在构造函数 #2 中,问题最严重:

public PaintballPlayer(String f, String l)
{
    this (f,"",l);
    id = count++;
    players = count++;
}

因为除了原始构造函数中的 2 次之外,您还将 count 的值增加了两次,因此总共增加了四次。

一旦你完成了 Devon Freese 所说的,你将不得不修改 PaintballPlayer 的构造函数

public PaintballPlayer(String first, String middle, String last)
{
    id = totalPlayers++;
    firstName = first;
    middleName = middle;
    lastName = last;
}
于 2013-04-22T17:54:15.830 回答
0

尝试这个。

import java.util.*;

public class PaintballPlayer {

    private String firstName, middleName, lastName;
    private String secFirst, secLast;
    private int id;
    private static int count;
    private static int totalPlayers;
    private int playerID;
    private static int players;

    public PaintballPlayer(String first, String middle, String last) {
        count++;
        id = count;
        players = count;
        firstName = first;
        middleName = middle;
        lastName = last;
    }

    public PaintballPlayer(String f, String l) {
        this(f, "", l);
        id = count;
        players = count;
    }

    public String toString() {
        String name = firstName + " " + middleName + " " + lastName;
        return name;
    }

    public static int getTotalPlayers() {
        totalPlayers = players;
        return totalPlayers;
    }

    public int getPlayerID() {
        playerID = count;
        return playerID;
    }
}

class PaintballPlayerTest {
//Test program for PaintballPlayer assignment

    public static void main(String[] args) {
//Part 1 check constructor & toString --(make sure ID is working too)
        PaintballPlayer sheldon = new PaintballPlayer("Sheldon", "Lee", "Cooper");
        PaintballPlayer leonard = new PaintballPlayer("Leonard", "Hofstadter");
        PaintballPlayer amy = new PaintballPlayer("Amy", "Farrah", "Fowler");

        System.out.println(sheldon);
        System.out.println(leonard);

//Part 2 test getTotalPlayer --should be 3
        System.out.println("The team has this many players " + PaintballPlayer.getTotalPlayers());
    }
}
于 2013-04-22T18:14:39.403 回答