c - 如何在 C 中完成这项任务?第2部分

标签 c terminate

以下提示输入半径和高度,并使用这些值计算圆柱体的体积。我如何编写这个程序,以便当用户为任一高度半径输入负值时它不会终止?范围必须在 1 到 10 之间,并且不允许其他提示。仅当输入非数字内容时才必须终止循环。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


float areaCirc(float r){
return (M_PI*r*r);
}

float volCyl(float r, float h){
return (areaCirc(r)*h);
}


int main(void) {
float r, h;
int k = 0;
float volume;
float avgh = 0;
float toth = 0;

do{
    float exit = scanf("%f%f", &r, &h);
    if (exit == 0) 
    {break;}
    if(r<=0 || r>10){
        printf("Invalid radius: %.2f\n",r);
    }
    if(h<=0 || h>10){
        printf("Invalid height: %.2f\n",h);
    }
    if(r>=0 && r<=10 && h>=0 && h <= 10){
    volume = volCyl(r,h);
    k = k++;
    printf(" Cylinder %d radius %.2f height %.2f volume %.2f\n",k,r,h,volume);
    toth = toth + h;
} }while(r>0 && h>0);
    avgh = toth/k;
    printf("Total Height: %.2f\n",toth);
    printf("Average Height: %.2f\n",avgh);

return EXIT_SUCCESS;
}

最佳答案

查看 while() 中的语句。请注意,当且仅当这些条件为真时,这将继续循环。

关于c - 如何在 C 中完成这项任务?第2部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19342202/

相关文章:

c - 使用 gold vs ld 链接器时使用的 glibc/pthreads 的不同符号

c - 试图在 C 中交换字符串?

c - 将结构数组传递给函数?

java - 查找数组中的对数,其中 A[i] * A[j] > A[i] + A[j]

multithreading - 为什么在等待我告诉终止的线程时会收到 "The handle is invalid"?

c - 优化给定总和的数组中对的索引

java - 为什么这个 Java 循环不终止?

powershell - 在 powershell 脚本终止时运行代码以关闭文件?

Python多处理类型错误: join() takes exactly 1 argument (2 given)