c++game2.0 猜数
2025-08-08 14:33:05
发布于:四川
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0)); // Seed random number generator
int secretNumber = rand() % 100 + 1; // Random number between 1-100
int guess = 0;
int attempts = 0;
cout << "I'm thinking of a number between 1 and 100.\n";
cout << "Can you guess what it is?\n\n";
do {
cout << "Enter your guess: ";
cin >> guess;
attempts++;
if (guess > secretNumber) {
cout << "Too high! Try again.\n";
} else if (guess < secretNumber) {
cout << "Too low! Try again.\n";
} else {
cout << "\nCongratulations! You guessed the number in "
<< attempts << " attempts!\n";
}
} while (guess != secretNumber);
return 0;
}
这里空空如也
有帮助,赞一个