我是编程的新手,所以请原谅我的无知。我在这个网站上没有得到正确的答案。这可能是我的搜索能力。在 C 中,我编写了一个运行良好的代码。但是我想按照用户希望的方式运行代码。这意味着,假设在执行三角形区域问题后,用户可以一次又一次地运行程序。这里需要做什么?这是代码:
#include <stdio.h>
#include <conio.h>
main()
{
char a;
int base, hight, radius, length, width;
float area, pi=3.14;
printf("\n\tEnter T to execute the area of Triangle"
"\n\tEnter R to execute the area of Rectangle"
"\n\tEnter C to execute the area of Circle\n\t\n\t\n\t\n\t\n\t");
a=getche();
printf("\n\t\n\t\n\t\n\t");
if(a=='T' || a=='t'){
printf("You want to know the Area of a Triangle."
"\n\tEnter your triangles Base: ");
scanf("%d", &base);
printf("\n\tEnter your triangles Hight: ");
scanf("%d", &hight);
printf("\n\tThe base is %d and the hight is %d."
"So the area of your triangle is: \n\n\t\t\t\t", base,hight);
area= .5*base*hight;
printf("%f", area);
}
else if(a=='R' || a=='r'){
printf("You want to know the Area of a Rectangle."
"\n\tEnter your rectangles Length: ");
scanf("%d", &length);
printf("\n\tEnter your rectangles Hight: ");
scanf("%d", &hight);
printf("\n\tThe length is %d and the hight is %d."
"So the area of your Rectangle is: \n\n\t\t\t\t", length,hight);
area= length*hight;
printf("%f", area);
}
else if(a=='C' || a=='c'){
printf("You want to know the Area of a Circle."
"\n\tEnter your circles Radius: ");
scanf("%d", &radius);
printf("\n\tThe Radius is %d."
"So the area of your Circle is: \n\n\t\t\t\t", radius);
area= pi*radius*radius;
printf("%f", area);
}else{
printf("Invalid Input");
}
getch();
}
Run Code Online (Sandbox Code Playgroud)
引入一个布尔变量,比如repeat。然后设置一个无限循环与do和while该重复,只要repeat是真实的。计算完成后,询问用户是否要继续;结果可以用scanf. 然后,相应地设置repeat,这意味着无限循环一旦repeat变为 false就终止。