如何排序相互依赖的函数? 在过去的两年里,我在学校里学习了C和C++的基础知识,我这个星期一刚开始新的一年,所以我想训练一点,因为我在假期里忘记了很多东西。p>
所以,我想在控制台中编写一个名为“或多或少”的小游戏。我必须猜测一个数字,对于我提出的每个数字,控制台会告诉我秘密数字是否更高。没什么疯狂的 我计划添加另一个迷你游戏和一些选项,所以我制作了一个菜单,只需点击一个数字,我就可以选择我想去的地方 问题是,我在游戏结束时做了一个重启功能,允许我选择是重启游戏还是返回菜单 因此,菜单功能在迷你游戏功能之后,但同时,重启功能在迷你游戏功能(这是一个while循环)和菜单功能之后 我想还有别的办法,但我不知道怎么做。我对此一无所知 我用英语描述我的代码,因为它是用法语写的(我是法国人)。如果你足够好的话,你可以不看译文就试试:) 感谢您抽出时间帮助世界各地的随机人群。我想你们都是超级信息学家:) 如果我在Shakspeare的语言中犯了一些错误,请不要犹豫告诉我 //迷你游戏功能如何排序相互依赖的函数? 在过去的两年里,我在学校里学习了C和C++的基础知识,我这个星期一刚开始新的一年,所以我想训练一点,因为我在假期里忘记了很多东西。p>,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;}
答案是菜单函数应该调用游戏函数,但游戏函数不应该调用菜单函数,游戏函数也不应该调用自身 为了避免游戏函数调用自身,可以使用无限循环:
while(1)
。这样,如果用户选择再次玩,循环将重新开始游戏。如果用户选择退出游戏,则一个简单的return
语句会将用户发送回菜单
菜单还需要一个无限循环。菜单需要某种方式来退出循环。因此,我将添加另一个菜单项,下面示例中的数字5。当用户选择5时,函数返回,退出循环
void Plus_ou_moins(void) { //More or less - the game
while (1) { // repeat the game until the user decides to stop
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) {
return; // if no, return to the menu
}
}
}
}
}
void Affichage_menu(void){
while (1) { // repeat the menu until the user decides to exit
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");
printf("5. Sortir\n");
Choix(); //I choose the number related to the game(1).
switch (choix) {
case 1:
Plus_ou_moins(); //I START THE GAME
break;
case 5:
return; // exit the menu
}
}
}
void Plus_ou_moins(void){//或多或少-游戏
而(1){//重复游戏,直到用户决定停止为止
Selection_nombre_mystere();//定义错误编号的函数
printf(“\nRentrez un nombre pour committer\n”);//输入一个数字开始
printf(“\n”);
while(nombrentre!=nombreMystere){//while提议的nb!=神秘的nb
scanf(“%d”,&nombrentre);//我读取并将值堆叠在变量中
if(nombreMystere>nombreEntre)//如果数字不够高
printf(“+\n”);//写入+
else if(nombreMystere
int重新启动=0;
printf(“你是新来的吗?”);//再试一次?
scanf(“%d”,&restart);//1=yes 0=no
如果(重新启动==0){
return;//如果否,返回菜单
}
}
}
}
}
无效附加菜单(无效){
while(1){//重复该菜单,直到用户决定退出
printf(“===菜单===\n”);//显示内容
printf(“\n”);
printf(“1.加上ou moins\n”);
printf(“2.Pour combien\n”);
printf(“3.Options\n”);
printf(“4.Statistiques\n”);
printf(“5.Sortir\n”);
Choix();//我选择与游戏相关的数字(1)。
开关(choix){
案例1:
Plus_ou_moins();//我开始游戏
打破
案例5:
return;//退出菜单
}
}
}
void Plus_ou_moins(void) { //More or less - the game
while (1) { // repeat the game until the user decides to stop
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) {
return; // if no, return to the menu
}
}
}
}
}
void Affichage_menu(void){
while (1) { // repeat the menu until the user decides to exit
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");
printf("5. Sortir\n");
Choix(); //I choose the number related to the game(1).
switch (choix) {
case 1:
Plus_ou_moins(); //I START THE GAME
break;
case 5:
return; // exit the menu
}
}
}