所以这是我在 C++ 中的参考代码:
#include <iostream>
#include <string>
class FIFAPlayer {
public:
std::string name;
std::string club;
int goals;
int age;
int salary;
int calculateScore() {
int goalsScore = goals * 80 / 100; // Goals are most valuable factor in futbol, goals win games
int ageScore = age * 20 / 100; // Age is subtracted since older players are at higher risk of not performing
int salaryScore = salary *20 / 100; // Salary is added since salary determines how much a player is worth
return goalsScore - ageScore + salaryScore; // sum up the three scores to get the player overall (work in progress, I want it to be different than current video games as well as simple for the code)
}
};
int main() {
int choice;
std::cout << "Enter 1 to see the statistics of the top ten FIFA soccer players or 2 to trade players and anazlyze the trade: "<< '\n';
std::cin >> choice;
if (choice == 1) {
FIFAPlayer players[10] = {
{"Erling Haaland", "Manchester City", 40, 22, 150},
{"Harry Kane", "Tottenham", 23, 28, 120},
{"Victor Osimhen", "Napoli", 21, 23, 80},
{"Kylian Mbappe", "Paris Saint-Germain", 24, 23, 200},
{"Robert Lewandowski", "Barcelona", 17, 33, 100},
{"Lionel Messi", "Paris Saint-Germain", 14, 35, 300},
{"Karim Benzema", "Real Madrid", 14, 33, 90},
{"Neymar", "Paris Saint-Germain", 13, 29, 220},
{"Mohamed Salah", "Liverpool", 28, 29, 150},
{"Ben Yedder", "AS Monaco", 17, 30, 60}
};
for (int i = 0; i < 10; i++) {
std::cout << i+1 << ". " << players[i].name << " (" << players[i].age << " years old) - " << players[i].club << " (" << players[i].goals << " goals) - $" << players[i].salary << "m - Val:1-65: " << players[i].calculateScore() << std::endl<<'\n';
}
}
else if (choice == 2) {
FIFAPlayer players[10] = {
{"Erling Haaland", "Manchester City", 40, 22, 150},
{"Harry Kane", "Tottenham", 23, 28, 120},
{"Victor Osimhen", "Napoli", 21, 23, 80},
{"Kylian Mbappe", "Paris Saint-Germain", 24, 23, 200},
{"Robert Lewandowski", "Barcelona", 17, 33, 100},
{"Lionel Messi", "Paris Saint-Germain", 14, 35, 300},
{"Karim Benzema", "Real Madrid", 14, 33, 90},
{"Neymar", "Paris Saint-Germain", 13, 29, 220},
{"Mohamed Salah", "Liverpool", 28, 29, 150},
{"Ben Yedder", "AS Monaco", 17, 30, 60}
};
// Display the current players and their clubs
std::cout << "Current players: " << std::endl;
for (int i = 0; i < 10; i++) {
std::cout << i+1 << ". " << players[i].name << " - " << players[i].club << std::endl;
}
// Prompt user for trade details
int player1Index, player2Index;
std::string club1, club2;
std::cout << "Enter the index of the first player to trade (1-10): ";
std::cin >> player1Index;
while (player1Index < 1 || player1Index > 10) {
std::cout << "Invalid index. Enter a number from 1 to 10: ";
std::cin >> player1Index;
}
std::cout << "Club 1 or Club 2: ";
std::cin >> club1;
std::cout << "Enter the index of the second player to trade (1-10): ";
std::cin >> player2Index;
while (player2Index < 1 || player2Index > 10) {
std::cout << "Invalid index. Enter a number from 1 to 10: ";
std::cin >> player2Index;
}
std::cout << "User 1 or User 2: ";
std::cin >> club2;
// Find the players and update their clubs
FIFAPlayer player1 = players[player1Index-1];
FIFAPlayer player2 = players[player2Index-1];
if (player1.club == club1 && player2.club == club2) {
player1.club = club2;
player2.club = club1;
players[player1Index-1] = player1;
players[player2Index-1] = player2;
// Display the updated players and their clubs
std::cout << std::endl;
std::cout << "Trade successful!" << std::endl;
std::cout << player1.name << " now plays for " << player1.club << std::endl;
std::cout << player2.name << " now plays for " << player2.club << std::endl;
std::cout << "Value of Trade: " << std::endl;
}
else {
std::cout << "Invalid choice" << std::endl;
}
return 0;
} }
因此代码运行正确,但我无法弄清楚为什么“无效选择”的 else 语句始终是输出并且它从不提供成功的交易输出。另外,我知道我需要添加两次选择同一玩家的异常处理和实际交易分数计算。
我已经尝试了一切,我是新手/新编码员所以请耐心等待。我需要一些建议