我正在学习java,现在正在处理循环。我写了这段代码:
players[0] = new Player(1, "Paul", "Point Guard");
players[1] = new Player(24, "Bryant", "Shooting Guard");
players[2] = new Player (6, "James", "Small Foward");
players[3] = new Player (21, "Duncan", "Power Foward");
players[4] = new Player (12, "Howard", "Center");
players[5] = new Player (6, "Erving", "Small Foward");
public String name()
{
int number = readNumber(); // read in the number
String s = "";
for(Player player: players){
// check if input number match with any number of the players
if(player.numberMatches(number))
s += player.getName() + " ";
else s = error();
}
return s;
}
error() 方法只是返回一个“没有玩家有这个号码”我的数组中有两个玩家有相同的号码;当我调用 name() 它返回“没有玩家有这个 nameErving”我试图删除 else 语句并且一切正常。为什么?有人可以帮我吗?