#include <iostream> int main() { int matches = 20; // 初始火柴数量 std::cout<<"How many ???"<<std::endl; std::cin>>matches; while (matches > 0) { int num; std::cout << "Number of matches left: " << matches << std::endl; std::cout << "Number of matches left Player 1, please enter the number of matches taken out (1 or 2 or 3 or 4): "; std::cin >> num; // 检查输入的合法性 if (num != 1 && num != 2 && num != 3 && num != 4) { std::cout << "Input it again" << std::endl; continue; } matches -= num; // 检查是否已经输掉游戏 if (matches <= 0) { std::cout << "Player 1 wins" << std::endl; break; } std::cout << "Number of matches left: " << matches << std::endl; std::cout << "Number of matches left Player 2, please enter the number of matches taken out (1 or 2 or 3 or 4): "; std::cin >> num; // 检查输入的合法性 if (num != 1 && num != 2 && num != 3 && num != 4) { std::cout << "Input it again" << std::endl; continue; } matches -= num; // 检查是否已经输掉游戏 if (matches <= 0) { std::cout << "Player2 wins" << std::endl; break; } } return 0; }
Note.ms
/quhuochaiyouxi