Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(1)
#include
using namespace std;
int main() {
int num1, num2;
int operation;
int result;
cout << "请输入第一个整数: ";
cin >> num1;
cout << "请输入第二个整数: ";
cin >> num2;
cout << "请选择操作 (1 表示加法, 2 表示减法): ";
cin >> operation;
if (operation == 1) {
result = num1 + num2;
cout << "结果: " << num1 << " + " << num2 << " = " << result << endl;
} else if (operation == 2) {
result = num1 - num2;
cout << "结果: " << num1 << " - " << num2 << " = " << result << endl;
} else {
cout << "无效的操作选择,请输入 1 或 2。" << endl;
}
return 0;
}
(2)
#include
using namespace std;
int main() {
int number;
cout << "请输入一个整数: ";
cin >> number;
if (number % 2 == 0) {
cout << number << " 是偶数。" << endl;
} else {
cout << number << " 是奇数。" << endl;
}
return 0;
}
(3)
#include
#include // 包含 rand() 和 srand() 函数
#include // 包含 time() 函数
using namespace std;
int main() {
srand(time(0));
int target_number = rand() % 10 + 1;
int guess_count = 0;
int guess;
cout << "欢迎来到猜数字游戏!" << endl;
cout << "我已经想好了一个 1 到 10 之间的数字。" << endl;
while (true) {
cout << "请输入你的猜测: ";
cin >> guess;
guess_count++;
if (guess < target_number) {
cout << "猜小了!" << endl;
} else if (guess > target_number) {
cout << "猜大了!" << endl;
} else {
cout << "恭喜你,猜对了!你总共猜了 " << guess_count << " 次。" << endl;
break;
}
}
return 0;
}