我看不出我在这里做错了什么,或者如果没有这样声明,这里的任何东西都是静态的。我只需要在这里指出正确的方向。
测试代码:
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() 方法。
编辑:这是我编辑的代码应用提供的修复。谢谢!