在 C 中的另一个开关中使用 Switch 语句
Using Switch statement in another switch in C
所以我正在尝试制作一个程序,主要是出于好奇,还有一点为了我的学习!但是我遇到了这个问题。当我尝试在另一个程序中使用 switch 语句时,如果我选择 switch 语句所在的情况,我的程序将进入默认状态。
printf("Please enter the first character of the thing you want to get perimeter and area of off: ");
scanf_s("%c", &holder);
switch (toupper(holder))
{
case 'C':
printf("Please enter the radius of the circle: ");
scanf_s("%d", &a);
if (a <= 0)
printf("Please enter a positive radius!\n");
else
{
perimeter = 2 * PI * a;
area = PI * a * a;
printf("Perimeter of the circle is %.2f\n", perimeter);
printf("Area of the circle is %.2f\n", area);
}
break;
case 'R':
printf("Please enter the two sides of the rectangle!\n");
scanf_s("%d %d", &a, &b);
if (a <= 0 || b <= 0)
printf("Please enter positive numbers as sides values!\n");
else
{
perimeter = a + a + b + b;
area = a * b;
printf("Perimeter of the rectangle is %.2f", perimeter);
printf("Area of the rectangle is %.2f", area);
}
break;
case 'T':
printf("Please enter a for calculation using 3 sides and b for 2 sides and the angle between them!");
scanf_s("%c", &e);
switch (e)
{
case 'a':
printf("Please enter the three sides values!\n");
scanf_s("%d %d %d", &a, &b, &c);
if (a <= 0 || b <= 0 || c <= 0 || a >= b + c || b >= a + c || c >= a + b || a < abs(b - c) || b < abs(a - c) || c < abs(a - b))
printf("Please enter viable side values!\n");
else
{
perimeter = a + b + c;
d = perimeter / 2;
area = sqrt(d * (d - a) * (d - b) * (d - c));
printf("Triangles perimeter is %2.f\n", perimeter);
printf("Triangles area is %2.f\n", area);
}
break;
case 'b':
printf("Please enter 2 sides and the degree between them!\n");
scanf_s("%d %d %d", &a, &b, &c);
c1 = PI / 180 * c;
perimeter = (a * a) + (b * b) - (2 * a * b * cos(c1));
area = (1 / 2) * a * b * sin(c1);
printf("Triangles perimeter is %2.f\n", perimeter);
printf("Triangles area is %2.f\n", area);
break;
}
default:
printf("Unknown character!\n");
}
system("pause");
}
你必须给出的第一个 scanf 输入是说 T\n
(因为你给了 T 后跟回车键 - 这是 linux 上的 \n
- 假设它是 linux)
因此当前标准输入缓冲区中不是一个而是两个字符。因此,当您扫描字符 'e'
时,它实际上读取的是 '\n'
而不是用户接下来输入的内容,因此进入内部开关中的默认情况。
编辑: 由于内部开关中没有默认情况,因此 \n
的情况不会在任何地方处理。这也意味着没有 break
为外部开关执行,因此所有后续情况包括 default 都将被执行,这就是为什么你得到 Unknown Character
因此,如果您确定总会有一个回车键按下,您可以在输入第二个字符之前执行 getchar()
。
getchar(); //removes \n from stdin
scanf_s("%c", &e);
所以我正在尝试制作一个程序,主要是出于好奇,还有一点为了我的学习!但是我遇到了这个问题。当我尝试在另一个程序中使用 switch 语句时,如果我选择 switch 语句所在的情况,我的程序将进入默认状态。
printf("Please enter the first character of the thing you want to get perimeter and area of off: ");
scanf_s("%c", &holder);
switch (toupper(holder))
{
case 'C':
printf("Please enter the radius of the circle: ");
scanf_s("%d", &a);
if (a <= 0)
printf("Please enter a positive radius!\n");
else
{
perimeter = 2 * PI * a;
area = PI * a * a;
printf("Perimeter of the circle is %.2f\n", perimeter);
printf("Area of the circle is %.2f\n", area);
}
break;
case 'R':
printf("Please enter the two sides of the rectangle!\n");
scanf_s("%d %d", &a, &b);
if (a <= 0 || b <= 0)
printf("Please enter positive numbers as sides values!\n");
else
{
perimeter = a + a + b + b;
area = a * b;
printf("Perimeter of the rectangle is %.2f", perimeter);
printf("Area of the rectangle is %.2f", area);
}
break;
case 'T':
printf("Please enter a for calculation using 3 sides and b for 2 sides and the angle between them!");
scanf_s("%c", &e);
switch (e)
{
case 'a':
printf("Please enter the three sides values!\n");
scanf_s("%d %d %d", &a, &b, &c);
if (a <= 0 || b <= 0 || c <= 0 || a >= b + c || b >= a + c || c >= a + b || a < abs(b - c) || b < abs(a - c) || c < abs(a - b))
printf("Please enter viable side values!\n");
else
{
perimeter = a + b + c;
d = perimeter / 2;
area = sqrt(d * (d - a) * (d - b) * (d - c));
printf("Triangles perimeter is %2.f\n", perimeter);
printf("Triangles area is %2.f\n", area);
}
break;
case 'b':
printf("Please enter 2 sides and the degree between them!\n");
scanf_s("%d %d %d", &a, &b, &c);
c1 = PI / 180 * c;
perimeter = (a * a) + (b * b) - (2 * a * b * cos(c1));
area = (1 / 2) * a * b * sin(c1);
printf("Triangles perimeter is %2.f\n", perimeter);
printf("Triangles area is %2.f\n", area);
break;
}
default:
printf("Unknown character!\n");
}
system("pause");
}
你必须给出的第一个 scanf 输入是说 T\n
(因为你给了 T 后跟回车键 - 这是 linux 上的 \n
- 假设它是 linux)
因此当前标准输入缓冲区中不是一个而是两个字符。因此,当您扫描字符 'e'
时,它实际上读取的是 '\n'
而不是用户接下来输入的内容,因此进入内部开关中的默认情况。
编辑: 由于内部开关中没有默认情况,因此 \n
的情况不会在任何地方处理。这也意味着没有 break
为外部开关执行,因此所有后续情况包括 default 都将被执行,这就是为什么你得到 Unknown Character
因此,如果您确定总会有一个回车键按下,您可以在输入第二个字符之前执行 getchar()
。
getchar(); //removes \n from stdin
scanf_s("%c", &e);