我不知道我在这里做错了什么,或者这里的任何事情都是静态的,如果没有这样说的话。我只需要指出正确的方向。
测试代码:
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()方法。
编辑:这是我编辑的代码,应用了提供的修补程序。谢谢!
答案 0 :(得分:3)
getTotalPlayers()
不是静态方法,因此您需要PaintballPlayer
的实例来调用此方法。
如果要将总玩家存储在PaintballPlayer
内,则需要一个静态属性(所有实例都有相同的参考):
class PaintballPlayer {
private static int totalPlayers;
public PaintballPlayer() {
totalPlayers++;
}
public static int getTotalPlayers() {
return totalPlayers;
}
}
答案 1 :(得分:0)
在你的代码中,构造函数#1
上存在一些问题public PaintballPlayer(String first, String middle, String last)
{
count = 0;
players = 0;
id = count++;
players = count++;
firstName = first;
middleName = middle;
lastName = last;
}
当变量计数增加两倍时,变量播放器和计数正在被重置。
在构造函数#2中,问题最严重:
public PaintballPlayer(String f, String l)
{
this (f,"",l);
id = count++;
players = count++;
}
因为除了原始构造函数中的2次之外,你的计数值增加了两倍,因此共计四次。
一旦你做了Devon Freese所说的你将不得不修改PaintballPlayer的构造函数
public PaintballPlayer(String first, String middle, String last)
{
id = totalPlayers++;
firstName = first;
middleName = middle;
lastName = last;
}
答案 2 :(得分: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());
}
}