我正在尝试用 C++ 制作剪刀石头布游戏。我在 codecademy.com 上测试了我的代码,它工作正常,但是当我在 codewars.com 上执行它时,它给了我这个错误:
main.cpp:29:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
这是我的代码:
#include <string>
#include <iostream>
std::string rps(const std::string& p1, const std::string& p2)
{
if (p1 == "rock" && p2 == "paper") {
return "Player 2 won!";
} else if (p1 == "rock" && p2 == "scissors") {
return "Player 1 won!";
} else if (p1 == "rock" && p2 == "rock") {
return "Draw";
} else if (p1 == "paper" && p2 == "rock") {
return "Player 1 won!";
} else if (p1 == "paper" && p2 == "scissors") {
return "Player 2 won!";
} else if (p1 == "paper" && p2 == "paper") {
return "Draw";
} else if (p1 == "scissors" && p2 == "rock") {
return "Player 2 won!";
} else if (p1 == "scissors" && p2 == "paper") {
return "Player 1 won!";
} else if (p1 == "scissors" && p2 == "scissors") {
return "Draw";
}
}
int main() {
std::cout << rps("rock", "scissors") << "\n";
std::cout << rps("rock", "paper") << "\n";
std::cout << rps("rock", "rock") << "\n";
std::cout << rps("scissors", "paper") << "\n";
std::cout << rps("scissors", "rock") << "\n";
return 1;
}