http://pastebin.com/xBqdUtTg操作數組(和一般的變量) - 主要 http://pastebin.com/BadasC7N - 構造如何instantiante並在構造函數類
我要創建程序增加了一個球員到一個數組構造文件的方法,但我不允許更改上述項目中給出的代碼片段。我的問題是,我不知道如何通過用戶輸入實例化具有正確大小的數組,因爲我必須堅持使用addAPlayer(String playername)的框架,並且不能添加參數來決定長度。此外,我不知道如何操縱變量在構造函數中
public class Practice{
public Practice(){
int x;
int y;
}
public static void main(String[] args){
Practice prac = new Practice();
prac.x = 1;
prac.y = 2;
}
}
不工作,所以其他什麼我是不可能操縱這些變量?在我被授予作業的存根代碼中有類變量,但它們被標記爲私有的,所以我不能直接更改它們,並且已經給出了構造函數的參數,所以我無法在其中定義它們
編輯:在主文件中也是for循環每次調用addAPlayer方法以設置通過用戶輸入輸入的正確數量的玩家名稱,但我怎樣才能確保每次調用該方法時它設置等於名稱
編輯不同的索引的數組:
public class NBATeam {
private String sTeamName;
private int nWin;
private int nLoss;
private String [] playerArray;
//Your code here
}//end of class definition
^^^構造
//Warning: Do not change the given codes
import java.util.Scanner;
public class NBA {
public static void main(String[] args) {
//construct Team Heat
NBATeam heat= new NBATeam("Heats");
System.out.print("How many players Heats own: ");
Scanner input = new Scanner (System.in);
int numberOfPlayers = input.nextInt();
// Prompt user to enter players into the Team
for (int i = 0; i < numberOfPlayers; i++) {
System.out.print("Enter the name of Player #" + (i + 1) + ": ");
String playerName = input.next();
heat.addAPlayer(playerName);
}
//construct Team Spurs
//Your code here
請將相關代碼複製到您的問題中,而不是提供pastebin鏈接。 –
您的發佈代碼顯示在構造函數中創建局部變量,這看起來不像您想要的。 –
你去了,不知道爲什麼每個人都不喜歡pastebin鏈接,儘管 –