Program exp3_3.c to complete the guessing game, which is an ancient code-breaking puzzle game. Specific rules: The computer randomly generates an integer from 1 to 100, and the player guesses until the guess is correct.

#include<iostream>
using namespace std;
#include<ctime>
int main()
{
srand((unsigned int)time(NULL));
int num = rand() % 100 + 1;
int val;
cout << "请玩家输入正确数字:" << endl;
while (1) {
    cin >> val;
    if (val == num) {
        cout << "玩家胜利  游戏结束" << endl;
        break;
    }
    else if (val > num) {
        cout << "提示:数字过大,请重新输入" << endl;
    }
    else {
        cout << "提示:数字过小,请重新输入" << endl;
    }
}
system("pause");
return 0;
}

Guess you like

Origin blog.csdn.net/weixin_52374973/article/details/110001575