作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我第一次在 StackOverflow 上发帖,但我已经访问过很多次了。我正在上计算机科学课,遇到了一个无法通过研究解决的问题。任务是创建一个带有实例变量和构造函数的 NBATeam 类,以及一个 NBA 客户端,该客户端从用户接收球员姓名参数并将它们传递到第一个类中的数组。
所以我遇到的问题是,当我运行程序时,我输入玩家名称并将其传递给数组,第一个名称数组被删除。不过,输入到最新数组中的名称没问题。
如果我的术语有误,我深表歉意,我还是个新手!
非常感谢您为我提供的任何帮助。
我将在下面插入我的代码。我的问题是它显示[null]
。另外,我为第一队输入了 bob,为第二队输入了 brian
,但 bob
在第二队的数组中。
请帮忙!
Create NBA team of Heats
Add a player to team Heats? (yes/no)
yes
Enter the name of the player:
bob
Add one more player to Heats?
no
Create NBA team of Spurs
Add a player to team Spurs? (yes/no)
yes
Enter the name of the player:
brian
Add another player to Spurs?
no
***Heat Wins!***
Heats[null]Wins: 4 Loses: 0
Spurs[bob]Wins: 0 Loses: 4
<小时/>
import java.util.*;
public class NBA {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String ifAddPlayer, playerName = null;
//construct Team Heat
System.out.println("Create NBA team of Heats");
NBATeam heats = new NBATeam("Heats");
System.out.println("Add a player to team Heats? (yes/no)");
ifAddPlayer = input.next();
while(ifAddPlayer.equalsIgnoreCase("yes")) {
System.out.println("Enter the name of the player: ");
heats.addPlayer(playerName);
playerName = input.next();
System.out.println("Add one more player to Heats?");
ifAddPlayer = input.next();
}
//construct team Spurs.
System.out.println("Create NBA team of Spurs");
NBATeam spurs = new NBATeam("Spurs");
System.out.println("Add a player to team Spurs? (yes/no)");
ifAddPlayer = input.next();
while(ifAddPlayer.equalsIgnoreCase("yes")) {
System.out.println("Enter the name of the player: ");
spurs.addPlayer(playerName);
playerName = input.next();
System.out.println("Add another player to Spurs?");
ifAddPlayer = input.next();
}
playAGame(heats,spurs);
System.out.println(heats.toString());
System.out.println(spurs.toString());
}
public static void playAGame(NBATeam heats, NBATeam spurs) {
for(int game = 0; game < 7; game++){
double score = Math.random();
if (score < .5) {
heats.winAgame(spurs);
}
else{
spurs.winAgame(heats);
}
if(spurs.nWin()==4 || heats.nWin()==4) {
break;
}
}
if(spurs.nWin()>heats.nWin()) {
System.out.println("***Spurs Win!***");
}
else
System.out.println("***Heat Wins!***");
}
}
import java.util.Arrays;
public class NBATeam {
private String sTeamName;
private int nWin;
private int nLoss;
private int nPlayer;
private String [] playerArray = new String[nPlayer];
public NBATeam(String sTeamName) {
this.sTeamName = sTeamName;
nPlayer=0;
}
public int nWin() {
return nWin;
}
public int nLoss() {
return nLoss;
}
public String getTeamName(){
//gets team's Name
return sTeamName;
}
public String toString() {
return sTeamName + Arrays.toString(playerArray)+ "Wins: " + nWin +
" Loses: " + nLoss;
}
public void lossAgame() {
this.nLoss++;
}
public void winAgame(NBATeam team2) { //To win over the team
nWin++;
team2.setLoss(team2.nLoss()+1);
//teamB.lossAgame();
}
public int getLossNumber() {
return this.nLoss;
}
public void setLoss(int l) {
this.nLoss=l;
}
public int getWinNumber() {
return this.nWin;
}
public void setWin(int w) {
this.nWin=w;
}
public void addPlayer(String player) {
String temp[] = playerArray;
playerArray = Arrays.copyOf(temp, temp.length+1);
playerArray[nPlayer] = player;
nPlayer++;
}
public String[] getPlayerArray() {
return playerArray;
}
public void setPlayerArray(String[] playerArray) {
this.playerArray = playerArray;
}
}
最佳答案
您从 playerName
中的初始值 null
开始,然后在获取该值之前添加 playerName
。这个,
heats.addPlayer(playerName);
playerName = input.next();
反转。应该是
playerName = input.next();
heats.addPlayer(playerName);
而且,你始终如一。这个,
spurs.addPlayer(playerName);
playerName = input.next();
同样是相反的。并且应该是
playerName = input.next();
spurs.addPlayer(playerName);
我建议您限制变量的可见性。如果你有
String playerName = input.next();
heats.addPlayer(playerName);
这会限制在设置之前添加播放器的可能性。
关于java - 如何将参数传递给另一个类中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51276328/
我是一名优秀的程序员,十分优秀!