编写java程序用于记录球员的信息(队名、球员姓名、球员号码)和比赛进球数
时间: 2023-08-16 11:03:49 浏览: 141
有JAVA编写的运动员档案管理系统
好的,我可以帮您编写这个Java程序。以下是示例代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Player {
private String teamName;
private String playerName;
private int playerNumber;
private int goalsScored;
public Player(String teamName, String playerName, int playerNumber) {
this.teamName = teamName;
this.playerName = playerName;
this.playerNumber = playerNumber;
this.goalsScored = 0;
}
public void scoreGoal() {
this.goalsScored++;
}
public String getTeamName() {
return teamName;
}
public String getPlayerName() {
return playerName;
}
public int getPlayerNumber() {
return playerNumber;
}
public int getGoalsScored() {
return goalsScored;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Player> players = new ArrayList<>();
// 获取球员信息
System.out.print("请输入球员信息(队名、球员姓名、球员号码),输入exit结束:");
while (true) {
String input = scanner.nextLine();
if (input.equals("exit")) break;
String[] info = input.split(" ");
String teamName = info[0];
String playerName = info[1];
int playerNumber = Integer.parseInt(info[2]);
Player player = new Player(teamName, playerName, playerNumber);
players.add(player);
}
// 记录比赛进球数
System.out.print("请输入进球的球员号码,输入exit结束:");
while (true) {
String input = scanner.nextLine();
if (input.equals("exit")) break;
int playerNumber = Integer.parseInt(input);
boolean found = false;
for (Player player : players) {
if (player.getPlayerNumber() == playerNumber) {
player.scoreGoal();
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到对应号码的球员!");
}
}
// 输出球员信息和比赛进球数
for (Player player : players) {
System.out.println(player.getTeamName() + " " + player.getPlayerName() +
"(号码:" + player.getPlayerNumber() + ")的进球数为:" + player.getGoalsScored());
}
}
}
```
您可以在控制台中输入球员信息和比赛进球数,程序将记录球员信息和比赛进球数,并输出每个球员的进球数。注意,这只是一个简单的示例程序,您可以根据自己的需求进行修改和扩展。
阅读全文