HelloWorld 发表于 2024-9-9 14:09:40

用 C++ 写了个小游戏

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int num, guess, tries = 0;
srand(time(0));
num = rand() % 100 + 1;
do {
    cout << "Enter a number between 1 and 100 : ";
    std::cin >> guess;
    tries++;
    if (guess > num)
      std::cout << "Too high! Try again.\n\n";
    else if (guess < num)
      std::cout << "Too low! Try again.\n\n";
    else
      std::cout << "Correct! You guessed it in " << tries << " tries!\n";
} while (guess != num);
return 0;
}

mrguess 发表于 2024-9-9 18:29:00

想起当年上学时的《数据结构》课
页: [1]
查看完整版本: 用 C++ 写了个小游戏