例一://按"1"、"2"控制
#include"stdio.h"
#include"conio.h"
voiddelay(intx)
{inty;
while(x--)
for(y=0;y<125;y++)
{;}
}
voidpout(inti)
{
if(i==1)
{
printf("%c",26);
delay(1000000);
printf("\b%c",0);
}
if(i==2)
{
printf("\b%c",0);
printf("%c",27);
delay(1000000);
printf("\b%c\b\b",0);
}
}
intmain()
{
inta=1;
while(1)
{
if(kbhit())
{
a=getch()-48;
}
pout(a);
}
return0;
}
例二:猜一個1~到100的數
#include<iostream>
#include<cstdlib>
#include<ctime>
usingnamespacestd;
intmain()
{
srand(time(0));//生成隨機數生成器種子
inttheNumber=rand()%100+1;//1-100的隨機數
inttries=0,guess;
cout<<"\tWecometoGuessMyNumber\n\n";
do
{
cout<<"Enteraguess:";
cin>>guess;
++tries;
if(guess>theNumber)
cout<<"Toohigh!\n\n";
if(guess<theNumber)
cout<<"Toolow!\n\n";
}while(guess!=theNumber);
cout<<"\nThat'sit!Yougotitin"<<tries<<"guess!\n";
return0;
}