本人自学写的代码,遇到一些问题,请会的帮我解释下(小白别进)
//怎样限制只获取一个字符,scanf()的缓冲输入要如何不干扰数据;//各函数是怎样传递返回值的;
//bank();用清除缓冲输入则无法计算值,怎样解决;
//请会的为我详细解答,非常感谢;详见代码(具体问题已标记):
#include<stdio.h>
#include<windows.h>
#include<stdint.h>
#include<time.h>
#define cls system("cls")
#define loa "loading..."
int bag();
int shop();
int killer();
int gold_add();//控制一秒加一的函数。
int gw();
int menu();//主菜单
int bank();
int exp=100,level=1,gold=100;//全局变量
int main()
{
cls;
system("color 0A");
int sp=0,rp=0;
char ar;
system("title [arbit]");
while(sp<60){
sp++;printf("*");
}
printf("\nG:OPEN\n");
printf("enter the ch_t for app\n");
while(rp<60){
rp++;printf("*");
}
printf("\n\n\n");
while(1){
ar=getchar();
if(ar=='g'||ar=='G')
{printf("%s",loa);menu();}
else
{
printf("\nagain\n");main();
}
}
}
int menu()
{
cls;
system("color 0A");
int sp=0,rp=0;
char ar;
while(sp<60){
sp++;printf("*");
}
printf("\nB:BAG-----------S:SHOP-----------E:KILLER\n");
printf("\nD:BANK---------------------------Q:QUIT\n");
printf("enter the ch_t for app(q to back)\n");
while(rp<60){
rp++;printf("*");
}
puts("\n\n");
puts("tip: if you lose, you can look up the code");
printf("\n\n\n");
printf("\nYour gold:%d\n-----Your exp:%d\n",gold,exp);
while(1){
ar=getchar();
if(ar=='b'||ar=='B'){
printf("%s",loa);bag();
}
else if(ar=='s'||ar=='S'){
printf("%s",loa);shop();
}
else if(ar=='e'||ar=='E'){
printf("%s",loa);killer();
}
else if(ar=='d'||ar=='D'){
printf("%s",loa);bank();
}
else if(ar=='q'||ar=='Q')
main();
else{
printf("error\n");menu();
}
}
}
int bag()
{
cls;
system("color 0B");
int as=0,ap=0;
char qw;
while(as<60){
as++;printf("*");
}
printf("\nYour gold is:%d\n",gold+gold_add());
gold=gold+gold_add();
while(ap<60){
ap++;printf("*");
}
printf("\nenter R to menu\n");
qw=getchar();
if(qw=='r'||qw=='R')
main();
}
int gold_add()
{
int qq=0;
while(1){
qq++;
Sleep(1000);
break;
}
return qq;
}
int shop()
{
cls;
system("color 0C");
int as=0,ap=0;
char qa;
while(as<60){
as++;printf("*");
}
printf("\nYour exp is:%d\n",exp);
while(ap<60){
ap++;printf("*");
}
printf("\nenter R to menu\n");
qa=getchar();
if(qa=='r'||qa=='R')
menu();
}
int killer()
{
cls;
system("color 0A");
int as=0,ap=0,gk;
while(as<60){
as++;printf("*");
}
printf("\nYour level is %d \nyou kill gw is %d\n",level,gw);
while(ap<60){
ap++;printf("*");
}
printf("\nenter R to menu\n");
gk=getchar();
if(gk=='r'||gk=='R')
menu();
}
int gw()
{
int qq=0;
while(1){
qq++;
Sleep(1000);
break;
}
return qq;
}
int bank()
{
cls;
int ar=0,pr=0;
static int money=0;
static int save=0;//存储输入值的变量
int s_mon,t_mon;
char as;
system("color 1A");
while(ar<60){
printf("*");ar++;
}
printf("\nYour bank have:%d\n",money+save);
puts("enter A|B to money(R to quit)");
while(pr<60){
printf("*");pr++;
}
printf("\n\n\n");
if(gold){
while(1){
as=getchar();
if(as=='a'||as=='A'){
printf("\n\nIn put your money to save:");
scanf("%d",&s_mon);
if (gold>=s_mon){
gold-=s_mon;
save+=s_mon;
money=save;
}//已解决迭代两次问题,但数值依旧计算两次
else{
printf("your money not en_e\n");
bank();
}
}
else if(as=='b'||as=='B'){
printf("\n\nIn put your money to take:");
scanf("%d",&t_mon);//第三次调用这个函数,则会变成加且输出错误提示
if(money<t_mon){
printf("you can't take:%d\n",t_mon);
Sleep(1000);
bank();
}
else{
gold+=t_mon;
save-=t_mon;
money-=save;
}
}
else if(as=='r'||as=='R'){
printf("BYE\n");menu();
}
else
{
printf("Again!!!\n");
bank();
}//第三次调用函数则输出这句话。为何??
}
}
}
[此贴子已经被作者于2016-11-13 01:39编辑过]