C中是否有try语句?我想制作一个梯形的“面积计算器”。我希望用户只输入数字:尝试在C中声明?
#include <stdio.h>
#include <conio.h>
main()
{
clrscr();
float base1, base2, height, area;
printf("This program will compute the area of your trapezoid. Enjoy!");
printf("\n\tPlease enter the length of base1: ");
scanf("%f", &base1);
printf("\tPlease enter the length of base2: ");
scanf("%f", &base2);
printf("\tPlease enter the length of height: ");
scanf("%f", &height);
area = (base1 + base2) * h/2;
printf("\nThe area of your trapezoid is: ", area);
getch();
}
但随后,他们输入非数字数据的自由。这样做会使程序崩溃。我能做些什么来检查它是否是正确的数据?我相信这可以通过一个循环来完成,但由于有一个程序有很多解决方案,我怀疑我的效率最高。
“列表X的”问题并不是一个非常适合于计算器的Q&你可以犯错**尝试的一种格式 – Flexo
**呢?尽管如此。如果你看,你可以很容易地找到答案。但是,您应该更改问题的第二部分以更加精确。 –