结合三元运算符帮我写点简单的C语言代码要求是已知父母的身高预测孩子的身高男孩身高=父亲的身高加母亲的身高×1082女孩身高=父亲身高+母亲身高×0923÷2
#include <stdio.h>
int main() { float father_height, mother_height, boy_height, girl_height; printf("请输入父亲的身高:"); scanf("%f", &father_height); printf("请输入母亲的身高:"); scanf("%f", &mother_height); // 计算男孩身高 boy_height = (father_height + mother_height) * 1.08 / 2; // 计算女孩身高 girl_height = (father_height + mother_height * 0.923) / 2; printf("男孩的身高为:%.2f厘米\n", boy_height); printf("女孩的身高为:%.2f厘米\n", girl_height); // 使用三元运算符判断孩子的性别并输出对应的身高 (boy_height > girl_height) ? printf("孩子的性别为男孩,身高为:%.2f厘米\n", boy_height) : printf("孩子的性别为女孩,身高为:%.2f厘米\n", girl_height); return 0; }
原文地址: https://www.cveoy.top/t/topic/bR0A 著作权归作者所有。请勿转载和采集!