gpt4 book ai didi

c - C 语言老虎机 (gotoxy)

转载 作者:行者123 更新时间:2023-11-30 21:35:42 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <time.h>
#include <windows.h>

int intSlot1, intSlot2, intSlot3;

void fnGotoXY(short x, short y);
void fnSlotMachine();
void fnSlot1();
void fnSlot2();
void fnSlot3();

int main(){

srand( time(0) );
fnSlotMachine();
fnSlot1();
fnSlot2();
fnSlot3();

}

void fnGotoXY(short x, short y){

COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}

void fnSlotMachine(){

fnGotoXY(5, 5);
printf(" x^---------------------------^x\n");
printf(" |oOoOoOoOoOoOoOoOoOoOoOoOoOoOo|\n");
printf(" \\_____________________________/\n");
printf(" /__$$$__\\ /__$$$__\\ /__$$$__\\");
fnGotoXY(5, 12);
printf(" <*^*^*^*> <*^*^*^*> <*^*^*^*>");


}

void fnSlot1(){

while(1){
Sleep(50);
fnGotoXY(5, 9);
intSlot1 = rand() % 9;
printf(" | %i %i %i |\n", intSlot1, intSlot1, intSlot1);
fnGotoXY(2, 10);
printf(" | %i %i %i |\n", intSlot1, intSlot1, intSlot1);
fnGotoXY(2, 11);
printf(" | %i %i %i |", intSlot1, intSlot1, intSlot1);
}
}

void fnSlot2(){

while(1){
Sleep(50);
fnGotoXY(17, 9);
intSlot2 = rand() % 9;
printf("| %i %i %i |\n", intSlot2, intSlot2, intSlot2);
fnGotoXY(17, 10);
printf("| %i %i %i |\n", intSlot2, intSlot2, intSlot2);
fnGotoXY(17, 11);
printf("| %i %i %i |", intSlot2, intSlot2, intSlot2);
}
}

void fnSlot3(){

while(1){
Sleep(50);
fnGotoXY(27, 9);
intSlot3 = rand() % 9;
printf("| %i %i %i |\n", intSlot3, intSlot3, intSlot3);
fnGotoXY(27, 10);
printf("| %i %i %i |\n", intSlot3, intSlot3, intSlot3);
fnGotoXY(27, 11);
printf("| %i %i %i |", intSlot3, intSlot3, intSlot3);
}
}

所以,我的问题是关于 gotoxy 的。在我放置 while(1) 循环后,其他插槽不会打印。希望得到一些回应。提前致谢!

最佳答案

所有函数中都有无限循环。如果您输入一个函数,您将永远不会返回。

考虑将其放在主函数中。

#include <stdio.h>
#include <time.h>
...

int main(){

srand( time(0) );
fnSlotMachine();
while(1) {
fnSlot1();
fnSlot2();
fnSlot3();
}
}

...
void fnSlot1(){

Sleep(50);
fnGotoXY(5, 9);
intSlot1 = rand() % 9;
printf(" | %i %i %i |\n", intSlot1, intSlot1, intSlot1);
fnGotoXY(2, 10);
printf(" | %i %i %i |\n", intSlot1, intSlot1, intSlot1);
fnGotoXY(2, 11);
printf(" | %i %i %i |", intSlot1, intSlot1, intSlot1);

}

void fnSlot2(){


Sleep(50);
fnGotoXY(17, 9);
intSlot2 = rand() % 9;
printf("| %i %i %i |\n", intSlot2, intSlot2, intSlot2);
fnGotoXY(17, 10);
printf("| %i %i %i |\n", intSlot2, intSlot2, intSlot2);
fnGotoXY(17, 11);
printf("| %i %i %i |", intSlot2, intSlot2, intSlot2);

}

void fnSlot3(){


Sleep(50);
fnGotoXY(27, 9);
intSlot3 = rand() % 9;
printf("| %i %i %i |\n", intSlot3, intSlot3, intSlot3);
fnGotoXY(27, 10);
printf("| %i %i %i |\n", intSlot3, intSlot3, intSlot3);
fnGotoXY(27, 11);
printf("| %i %i %i |", intSlot3, intSlot3, intSlot3);

}

关于c - C 语言老虎机 (gotoxy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27685617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com