请教一道c++的题目

来源:百度知道 编辑:UC知道 时间:2024/07/02 19:30:49
程序随机产生一个1到8这间的整数,让操作者猜这个数。从键盘输入猜的数后,如果猜测对了,屏幕显示成功信息;否则显示“太大”或“太小”的提示信息,并让操作者再次输入。3次不能猜中,则为失败
具体要求如下:
(1) 显示“猜数游戏大比拼”的标题
(2) 显示一个1到8这间的随机整数
(3) 屏幕提示“请输入你猜的数:”
(4) 猜测对了,屏幕显示成功信息
(5) 猜测不对,显示“太大”或“太小”的提示信息,并提示“请再输入你猜的数:”
(6) 3次不能猜中,则显示“你没有猜中!”

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

void main()
{
int num, gNum, success = 0;
cout << " **** 猜数游戏大比拼 **** " << endl << endl;
srand(time(NULL));
num = 1 + rand() % 7;
for( int i = 0; i < 3; i++ )
{
cout << " 第" << i+1 << "轮,请输入你猜的数:";
cin >> gNum;
if( gNum == num ) { success = 1; cout << " 恭喜你猜对了~" << endl; break; }
if( gNum < num ) { cout << " 你猜得小了。" << endl << endl; continue; }
if( gNum > num ) { cout << " 你猜得大了。" << endl << endl; continue; }
}
if( !success ) cout << " 你没有猜中,正确答案是:" << num << endl;
}

我不会C++怎么办?

不会

我也不会~~