【发布时间】:2021-12-23 06:35:02
【问题描述】:
#include <stdio.h>
#include<math.h>
int taskchoice, a;
void Menu() {
printf("What would you like to do: \n Fish = 1\n Hunt = 2 \n Cook = 3\n Boss = 4\n");
scanf("%d", &a);
if (a == 1) {
Fishing();
}
if (a == 2) {
printf("Hunting\n");
}
if (a == 3) {
printf("Cooking\n");
}
if (a == 4) {
printf("Bossing\n");
}
else {
abort();
}
}
void Fishing() {
printf("Time to fish me lord?\n");
return;
}
int main() {
Menu();
printf("Would you like to go back to the menu?");
//my fishing function is working as intended but the question is, why does it not display
//anything that is posted after the menu(); in the main function
//I am new to C and thank you for taking the time to look at my question.
}
【问题讨论】:
-
如果您输入除
4之外的任何值,则将调用abort()。程序是否缺少一些elses? -
是的,如果你没有选择正确的答案,它应该结束。我一直按1开始钓鱼功能。钓鱼功能有效,但程序没有返回主程序,因此我无法继续编程。
-
正如我写的,如果你输入
1那么Fishing()会被调用,然后因为你没有输入4,程序会abort()而不是返回main. -
else仅与最后一个if相关,而不是全部。 -
所以我遇到的问题是多个 if 语句而不是 else-if 语句?