如何排序彼此依赖的函数?

问题描述 投票:2回答:1

我在过去的两年中在学校里学习了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;
}}
c
1个回答
1
投票

答案是菜单功能应调用游戏功能,但游戏功能不应调用菜单功能,游戏功能不应调用自身。

为了避免游戏功能自行调用的情况,您可以使用无限循环:while(1)。这样,如果用户选择再次玩,循环将重新开始游戏。如果用户选择退出游戏,则简单的return语句会将用户返回菜单。

© www.soinside.com 2019 - 2024. All rights reserved.