[英]Run a program multiple times in C
我是編程的新手,所以請原諒我的無知。 我在這個網站上沒有得到正確答案。 這可能是我無法搜索的。 在C中,我編寫了一個運行良好的代碼。 但我想在用戶希望的時間內運行代碼。 這意味着,假設在執行Triangle區域問題后,用戶可以反復運行該程序。 這需要做什么? 這是代碼:
#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();
}
引入一個布爾變量,比如說repeat
。 然后設置了一個無限循環do
和while
一個重復只要repeat
是真實的。 在計算之后,詢問用戶他或她是否願意繼續; 可以使用scanf
讀取結果。 然后,相應地設置repeat
,這意味着一旦repeat
變為false,就會終止無限循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.