我正在尝试用 C 编写一个程序,该程序根据用户提供的输入调用两个函数之一。
如果用户输入'1',程序应该说“你选择了A”,如果用户输入'2',程序应该说“你选择了B”。我遇到的问题是,无论用户输入 1 还是 2,都会返回“您选择 A”的消息(请参见屏幕截图)。
这是我的代码:
include <stdio.h>
void celsiusFahrenheit()
{
printf("You chose A");
}
void fahrenheitCelsius()
{
printf("You chose B");
}
int main()
{
int selection;
printf("Please enter '1' to convert celsius to fahrenheit, or enter '2' to convert fahrenheit to celsius: ");
scanf_s("%d", &selection);
while (selection < 1 || selection > 2)
{
printf("Please enter a valid entry of either 1 or 2: ");
scanf_s("%d", &selection);
}
if (selection = 1)
{
celsiusFahrenheit();
}
else
{
fahrenheitCelsius();
}
}
如果您能提供任何帮助,我将不胜感激!