我已经创建了这个程序将华氏转换为摄氏温度,反之亦然,现在我想将这个if语句转换为switch语句,有人可以帮我完成这个任务。更改C语言中switch语句的代码?
int main(void) {
char condition; // Declare a variable.
float celsius, fahrenheit, temp;
printf("Enter Temp in either Celsius or Fahrenheit: \n"); scanf("%f", &temp); //Ask user to enter Temp.
printf("What type of conversion you want? (hint: 'C/c' for Celsius or 'F/f' for Fahrenheit) \n"); scanf(" %c", &condition);
if (condition == 'f' || condition == 'F') {
fahrenheit = (temp * 1.8) + 32; //Calculates temp in Fahrenheit.
printf("The temp in Fahrenheit is: %.2f", fahrenheit); //Displays result.
} else if (condition == 'c' || condition == 'C') {
celsius = (temp - 32)/1.8; //Calculate temp in Celsius.
printf("The temp in Celsius is: %.2f", celsius); //Displays result.
}
}
帮助你,可能。为你做,不。告诉我们你已经尝试了什么,并解释它是如何工作的。 –