输出我得到的是
choice1:convert the value of temperature in fahrenheit
choice2:convert the value of temperature in celsius
Enter the choice(1,2)1
enter the value in fahrenheit98.6
Process returned 0 (0x0) execution time : 7.313 s
Press any key to continue.
这是代码:
#include <stdio.h>
int main(void) {
//Program to convert fahrenheit and celsius temperature into viceversa
int choice;
float celsius,fahrenheit;
printf("choice1:convert the value of temperature in fahrenheit");
printf("\n choice2:convert the value of temperature in celsius");
printf("\nEnter the choice(1,2)");
scanf("%d",&choice);
if(choice==1)//control statements
{
printf("enter the value in fahrenheit");
scanf("%f",&fahrenheit);
celsius=(fahrenheit-32)/1.8?:printf("value in celsius is %f",celsius);
}
else if(choice==2)
{
printf("enter the value in celsius");
scanf("%f",&celsius);
fahrenheit=(celsius*1.8)+32?:printf("value in fahrenheit is %f",fahrenheit);//ternary operator used here
}
else
{
printf("/n wrong option");}
return 0;
}
//ternary operator used here
三元运算符逻辑有什么问题,因为我们知道三元运算符的syntac是condition?:expression1:expression2;
答案 0 :(得分:0)
您对条件运算符的使用没有意义。除非转换结果为0,否则printf
不会发生。即使这样,celsius
或fahrenheit
变量尚未设置,因为三元不是&# 39; t完成评估,因此他们打印他们最初包含的任何垃圾值。
您应该在单独的陈述中进行分配和打印结果。
celsius=(fahrenheit-32)/1.8;
printf("value in celsius is %f",celsius);
...
fahrenheit=(celsius*1.8)+32;
printf("value in fahrenheit is %f",fahrenheit);