#include <iostream>
#include <ctime>
#include <cstdlib>
int main() {
srand(time(0)); // 初始化随机数种子
int user_choice, computer_choice;
const char *choices[] = {"石头", "剪刀", "布"};
std::cout << "欢迎来到石头剪刀布游戏!
";
std::cout << "请输入你的选择(0-石头,1-剪刀,2-布):";
std::cin >> user_choice;
if (user_choice < 0 || user_choice > 2) {
std::cout << "无效的选择,请输入0、1或2。
";
return 1;
}
computer_choice = rand() % 3; // 生成计算机的随机选择
std::cout << "你选择了:" << choices[user_choice] << "
";
std::cout << "计算机选择了:" << choices[computer_choice] << "
";
if (user_choice == computer_choice) {
std::cout << "平局!
";
} else if ((user_choice + 1) % 3 == computer_choice) {
std::cout << "你赢了!
";
} else {
std::cout << "你输了!
";
}
return 0;
}
提示:务必是中文版c++
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0)); // 初始化随机数种子
int a, b;
char op;
int user_answer;
a = rand() % 10 + 1; // 生成1到10之间的随机整数a
b = rand() % 10 + 1; // 生成1到10之间的随机整数b
op = '+'; // 默认操作符为加法
// 随机选择操作符
if (rand() % 2 == 0) {
op = '-';
} else if (rand() % 2 == 0) {
op = '*';
}
std::cout << "请算算式:" << a << " " << op << " " << b << " = ?";
std::cin >> user_answer;
int correct_answer;
switch (op) {
case '+':
correct_answer = a + b;
break;
case '-':
correct_answer = a - b;
break;
case '*':
correct_answer = a * b;
break;
}
if (user_answer == correct_answer) {
std::cout << "恭喜你,答对了!";
} else {
std::cout << "很遗憾,答错了。正确答案是:" << correct_answer << " ";
}
return 0;
}