我已經創建了這個程序將華氏轉換爲攝氏溫度,反之亦然,現在我想將這個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.
}
}
幫助你,可能。爲你做,不。告訴我們你已經嘗試了什麼,並解釋它是如何工作的。 –