C++猜数字游戏源代码

2024-11-22 19:31:23
推荐回答(2个)
回答(1):

#include "stdio.h"
#include "stdlib.h"
void main()
{
int num=rand()%100;
int guess;
int i=0;
printf("Guess a number (1-100):");
while(1)
{
scanf("%d",&guess);
i++;
if(i>塌正=10)
{
printf("Sorry, game over!\n");
break;
}
if(guess==num)
{
printf("You are right, guess %d times.\n",i);
break;
}
else if(guess>坦衫源num)
printf("Guess too big, try again:");
else
printf("让态Guess too small, try again:");

}
}

回答(2):

#include
#include
using namespace std;
int main()
{
char choice;
int i=0;
int number;
int n;
cout<<"圆毕请猜1—100的整数:"<while(1)
{srand((unsigned )time(NULL));
n=rand()%100+1;
while(cin>>number)
{

i++;
if(i==10)
{
cout<<"你猜的次橘没芹数已达10次,此次结束。"< break;
}
if(number>n)
{
cout<<"你猜大了察斗"< continue;
}
else if(number {
cout<<"你猜小了"< continue;
}
else
{
cout<<"你猜对了"< break;
}
}
i=0;
do
{
cout<<"是否继续?是请按y/否请按n。"< cin>>choice;
}while(choice!='y' &&choice!='n');
if(choice=='n')
{
return 0;
}
}
}