我想创建一个程序,让用户可以选择构建歌曲(让用户输入持续时间和频率到数组中,然后选择选项b,播放该频率和持续时间)。我遇到的问题是让程序正确返回主菜单并让用户选择一个选项。
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
int main(void)
{
int i = 0;
char menu, a, A, b, B, c, C;
int duration[100];
float frequency[100];
printf("a. Build the song\n");
printf("b.Play the song\n");
printf("c. Quit\n");
printf("Please enter an option: ");
scanf(" %c", &menu);
if (menu == 'a' || menu == 'A') {
printf("Please enter a frequency: ");
scanf("%f", &frequency[i]);
printf("Please enter a duration: ");
scanf("%d", &duration[i]);
system("cls");
}
else if (menu == 'b' || menu == 'B') {
Beep(frequency[i], duration[i]);
system("cls");
return menu;
}
else if (menu != 'a' || menu != 'A' && menu != 'b' || menu != 'B' && menu != 'c' || menu != 'C') {
while (menu != 'a' || menu != 'A' && menu != 'b' || menu != 'B' && menu != 'c' || menu != 'C') {
printf("\n\nSorry that an invalid input\n\n");
printf("a. Build the song\n");
printf("b.Play the song\n");
printf("c. Quit\n");
printf("Please enter an option: ");
scanf(" %c", &menu);
}
}
else if (menu == 'c' || menu == 'C') {
return 0;
}
system("pause");
}
答案 0 :(得分:2)
您必须在循环中再次放置需要使用的代码。 1}}循环带有一个检查变量(你已经拥有它)就是你所需要的。
示例:
while