在过去的两年中,我在学校学习了C和C ++的基础知识,而本周一刚开始新的一年,所以我想进行一些培训,因为我在假期里忘记了很多东西。
因此,我想在控制台中编写一个“或多或少”的迷你游戏。我必须猜测一个数字,对于我建议的每个数字,控制台都会告诉我密码是否更高。没什么疯狂的。
我打算添加另一个迷你游戏和一些选项,所以我做了一个菜单,只需点击一个数字,我便选择了要去的地方。
问题是我在游戏结束时做了一个重启功能,该功能使我可以选择是否要重启游戏或返回菜单。
因此,菜单功能应在小游戏功能之后,但同时,重新启动功能应在小游戏功能(这是一个while循环)中,并且在菜单功能之后。
我认为还有另一种方法可以做到,但是我不知道如何做。我对此一无所知。
我用英语描述了我的代码,因为它是用法语(我是法语)编写的。如果您足够好,可以不阅读译文而尝试:)
感谢您抽出宝贵的时间来帮助世界各地的随机人群。你们都是超级信息学家,我猜:)
如果我在莎士比亚的语言中犯了一些错误,请毫不犹豫地告诉我。
//迷你游戏功能
int Plus_ou_moins() { //More or less - the game
Selection_nombre_mystere(); //function that defines the mistery number
printf("\nRentrez un nombre pour commencer\n"); //Enter a number to begin
printf("\n");
while (nombreEntre != nombreMystere) { //while proposed nb != mystery nb
scanf("%d", &nombreEntre); //I read then stack the value in a variable
if (nombreMystere > nombreEntre) //If the number isn't high enough
printf("+\n"); //Write +
else if (nombreMystere < nombreEntre) //the opposite
printf("-\n");
else if (nombreMystere == nombreEntre) { //I guessed the good number
printf("Bravo, vous avez trouve le nombre mystere: %d\n", nombreMystere); //THE RESTART PART IN THE MINI-GAME FUNCTION ->
int restart = 0;
printf("Voulez vous jouer de nouveau ? "); //try again ?
scanf("%d", &restart);//1=yes 0=no
if (restart == 0) {
Affichage_menu(); //I display the menu if it's no
} else {
Plus_ou_moins(); //I restart the game if it's yes
}
}
}
return 0;}
//菜单的显示功能
void Affichage_menu(){
printf("=== MENU ===\n"); //Display things
printf("\n");
printf("1. Plus ou moins\n");
printf("2. Pour combien\n");
printf("3. Options\n");
printf("4. Statistiques\n");
Choix(); //I choose the number related to the game(1).
switch (choix) {
case 1:
Plus_ou_moins(); //I START THE GAME = IMPOSSIBLE because the game is declared before. Even if I reverse the order of the 2 functions, the menu is not declared at the restart part.
break;
}}
答案 0 :(得分:1)
答案是菜单功能应调用游戏功能,但游戏功能不应调用菜单功能,游戏功能不应调用自身。
为避免游戏函数自行调用的情况,可以使用无限循环:-----------
5
-----------
8
。这样,如果用户选择再次玩,循环将重新开始游戏。如果用户选择退出游戏,则简单的upload-symbols -p ios -gsp GoogleService-Info.plist <path to xcarchive/dSYMs>
语句会将用户返回菜单。
菜单也需要无限循环。菜单需要某种方式退出循环。因此,我将添加另一个菜单项,在下面的示例中为5。当用户选择5时,函数返回,退出循环。
while(1)