猜数游戏 c语言编程题 C语言编写猜数字游戏

C\u8bed\u8a00\u7f16\u7a0b\uff1a\u7f16\u5199\u4e00\u4e2a\u731c\u6570\u7684\u6e38\u620f\uff0c\u7cfb\u7edf\u81ea\u52a8\u4ea7\u751f\u4e00\u4e2a\u968f\u673a\u6570\uff0c\u4f60\u6765\u731c\uff0c\u7a0b\u5e8f\u7ed9\u51fa\u63d0\u793a\uff0c\u76f4\u5230\u731c\u5bf9\u4e3a\u6b62\u3002

import java.util.*;
class Assignment8{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int x=(int)(Math.random()*100);//\u751f\u6210\u4e00\u4e2a0~100\u7684\u968f\u673a\u6570
int y=-1;
System.out.println("\u5df2\u751f\u62100~100\u7684\u968f\u673a\u6574\u6570\uff0c\u8bf7\u8f93\u5165\u60a8\u6240\u731c\u7684\u6570\uff1a");
while(x!=y)
{
y=sc.nextInt();
if(y>x)
{
System.out.println("\u8f93\u5165\u7684\u6570\u8fc7\u5927");
}
else if(y<x)
{
System.out.println("\u8f93\u5165\u7684\u6570\u8fc7\u5c0f");
}
}
System.out.println("\u6b63\u786e\uff01\u8be5\u968f\u673a\u6570\u662f"+x);
sc.close();
}
}

\u6269\u5c55\u8d44\u6599\uff1awhile\u5faa\u73af\u7684\u683c\u5f0f\uff1awhile(\u8868\u8fbe\u5f0f){\u8bed\u53e5\uff1b}
while\u5faa\u73af\u7684\u6267\u884c\u987a\u5e8f\uff1a\u5f53\u8868\u8fbe\u5f0f\u4e3a\u771f\uff0c\u5219\u6267\u884c\u4e0b\u9762\u7684\u8bed\u53e5\uff0c\u8bed\u53e5\u6267\u884c\u5b8c\u4e4b\u540e\u518d\u5224\u65ad\u8868\u8fbe\u5f0f\u662f\u5426\u4e3a\u771f\uff0c\u5982\u679c\u4e3a\u771f\uff0c\u518d\u6b21\u6267\u884c\u4e0b\u9762\u7684\u8bed\u53e5\uff0c\u7136\u540e\u518d\u5224\u65ad\u8868\u8fbe\u5f0f\u662f\u5426\u4e3a\u771f\u2026\u2026\u5c31\u8fd9\u6837\u4e00\u76f4\u5faa\u73af\u4e0b\u53bb\uff0c\u76f4\u5230\u8868\u8fbe\u5f0f\u4e3a\u5047\uff0c\u8df3\u51fa\u5faa\u73af\u3002
\u4f8b\uff1a
int a=NULL;
while(a<10){
a++;//\u81ea\u52a0
if(a>5)//\u4e0d\u7b49while\u9000\u51fa\u5faa\u73af\uff0c\u76f4\u63a5\u5224\u65ad\u5faa\u73af
{break;//\u8df3\u51fa\u5faa\u73af}
}
\u7ed3\u679c\uff1a\u7ed3\u675f\u540ea\u7684\u503c\u4e3a6\u3002

#include
#include
//#include
unsigned char Num[4] = {0},getNum[4] = {0x20,0x20,0x20,0x20};
void help(void)
{
char i =0,j = 0;
for(i =0;i<4;i++)
{
for(j = 0;j<4;j++)
if(Num[j] == getNum[i])
{
if(i==j)
printf("%dth is correct!\r\n",i);
else
{
printf("%d is a correct data\r\n",getNum[i]);
}

}
else
{
if(i == 3&&j==3)
{printf("/********************small game*****************/\
input 4 data gess the random data,input h can get help\
");}
}
}
}

void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0;
// { int a;a = 878*101;cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0;
/* Seed the random-number generator with GetTickCount so that
the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );


orginal = rand();
Num[3] = orginal%10000/1000;
Num[2] = orginal%1000/100;
Num[1] = orginal%1000%100/10;
Num[0] = orginal%10;
// printf(" %d",orginal);
for(i = 0;i<4;)
{

for(j = i+1;j<4;j++)
{
if(Num[i]==Num[j])
{ Num[i]++;if(Num[i]>9) Num[i] = 0;i=0;break;}
}
if(j == 4)i++;
}
get: for(i = 0;i<4;)
{
printf("input Num %d ",i);
scanf("%s",&getNum[i]);

if(getNum[i]-0x30>9||getNum[i]-0x30<0)
{
if (getNum[i] == 'H'||getNum[i] =='h')
{
help();
}
else
printf("input one num\r\n");
//if(i>0)i--;
}
else
{
getNum[i] -=0x30;
for(j = 0;j<i;j++)
{
if(getNum[i] == getNum[j])
{printf("have a same data,please input another\r\n"); break;}
}

if(j==i)
i++;
}
}
printf("you input num %d%d%d%d Y or N?\r\n",getNum[0],getNum[1],getNum[2],getNum[3]);
do{
scanf("%c",&j);
printf("%c",j);
if((j == 'N') || (j == 'n'))goto get;
}while( !((j == 'Y') || (j == 'y')));
printf("over");

}

#include <stdio.h>
#include <time.h> //产生随机数需要此头文件

void main()
{
int n, in, t; //n为随机数,in为猜的数,t为猜的次数
srand((unsigned)time(NULL));//初始化随机数
while(1)
{
n = rand() % 100 + 1; //产生随机数
t = 0;
while(1)
{
printf(">");
scanf("%d", &in); //输入猜的数
if(in > 100 || in < 1)//输入非法
{
printf("illegal input\n");
continue;
}

t++; //放在这个位置,输入非法不会增加猜的次数

if(in > n)//大了
{
printf("Big\n");
}
else if(in < n)//小了
{
printf("Small\n");
}
else//猜对了
{
printf("Right, %d time(s)\n", t);
break;
}

if(t > 9)
{
printf("over\n");
}
}
}
}

#include<iostream>
#include<math.h>
using namespace std;
void main()
{
char a;
for(a = 'c'; a != 'q';cin>>a)
{
int inNumber = 0;
int guessCount = 0;
int correctNumber = rand()%100;
for(int i = 0 ; i < 10 ; i++)
{
cout<<">";
cin>>inNumber;
if(inNumber>100 || inNumber < 1)
{
cout<<"illegal input"<<endl;
continue;
}
else if(inNumber == correctNumber)
{
cout<<"Right,"<<guessCount<<endl;
break;
}
else if(inNumber < correctNumber)
{
cout<<"Small!"<<endl;
guessCount++;
}
else
{
cout<<"Big!"<<endl;
guessCount++;
}
}
cout<<"Continue or quit?(c/q)";
}
}
c语言和c++差不多吧

efrqerqer

扩展阅读:3一6岁儿童单机游戏 ... 如何自己编程做游戏 ... 少儿编程小游戏 ... 吓人的编程代码 ... 扫一扫题目出答案 ... 自己编程做游戏的软件 ... 学编程一年大概多少钱 ... 少儿编程闯关游戏 ... c十十入门编程课程视频 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网