我對編程真的很陌生,所以如果這真的很基礎,我很抱歉。我正在用 Java 創建一個程式,該程式使用各種資料創建一個足球運動員,例如姓名、進球、比賽和助攻。然后將它們添加到足球隊課程中,我創建了一種添加、顯示和洗掉球員的方法,但我已經有幾個星期沒有做太多編程了,我很難弄清楚編輯。
這些是方法。
public void addPlayer() {
//creates a scanner to input the variables
Scanner userInput = new Scanner(System.in);
//creates a new player object
Player newPlayer = new Player();
// using the setters to enter the variables for the new player using the scanner
System.out.println("Please type the name of the player:");
newPlayer.setName(userInput.nextLine());
System.out.println("Please type the age of the player:");
newPlayer.setAge(userInput.nextInt());
System.out.println("Please type the number of goals the player has scored:");
newPlayer.setGoals(userInput.nextInt());
System.out.println("Please type the number of assists the player has:");
newPlayer.setAssists(userInput.nextInt());
System.out.println("Please type the number of games the player has played:");
newPlayer.setGamesPlayed(userInput.nextInt());
System.out.println("Please type the position that the player primarily plays:");
newPlayer.setPosition(userInput.next());
//once the variables are entered, we add the player object to the arraylist
playerList.add(newPlayer);
System.out.println("Player Successfully Added:");
}
public void showPlayers() {
//for loop to iterate through the arraylist of players, outputting each variable
for (int i = 0; i < playerList.size(); i ) {
System.out.println("Player " i "name: " playerList.get(i).getName() " Position: " playerList.get(i).getPosition() " age: " playerList.get(i).getAge() " number of goals: " playerList.get(i).getGoals()
" number of assists: " playerList.get(i).getAssists() " games played: " playerList.get(i).getGamesPlayed());
}
}
public void removePlayer() {
for (int i = 0; i < playerList.size(); i ) {
System.out.println("Player " i "name: " playerList.get(i).getName());
}
System.out.println("Enter the index of the player you want to remove");
Scanner userInput = new Scanner(System.in);
playerList.remove(userInput.nextInt());
System.out.println("Player Successfully Removed:");
for (int i = 0; i < playerList.size(); i ) {
System.out.println("Player " i "name: " playerList.get(i).getName());
}
(我已經意識到以前的方法應該呼叫 showPlayers 而不是做一個全新的回圈)
我現在真的在用 editPlayer 方法苦苦掙扎,我已經開始了,但我的大腦一片空白,任何幫助將不勝感激。我基本上希望能夠從 Show Players 方法中呼叫并讓用戶從 playerList 甚至他們的姓名中輸入索引,然后能夠編輯資料。
public void editPlayer() {
this.showPlayers();
Scanner userInput = new Scanner(System.in);
System.out.print("Enter the index of the player you would like to edit: ");
try {
Integer name = userInput.nextInt();
if (name = playerList.index()) {
}
}
}
uj5u.com熱心網友回復:
以下是使用給定索引從串列中更新其中一個播放器的實作。
public void editPlayer() {
this.showPlayers();
Scanner userInput = new Scanner(System.in);
System.out.print("Enter the index of the player you would like to edit: ");
try {
Integer index = userInput.nextInt();
Player playerToUpdate = null;
// Filter out the player from the list, that has to be updated
for(int i = 0; i < playerList.size(); i ){
if(i == index) {
playerToUpdate = playerList.get(i);
break;
}
}
if(playerToUpdate != null){
// Take input and update the data, you can ask for the data needs to be updated.
// For eg Name
System.out.println("Please type the name of the player:");
playerToUpdate.setName(userInput.nextLine());
}else{
// The input index is not a valid index
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462238.html
