# 简单的IF语句scanfSimple IF statement scanf

``````#include <stdio.h>

int main()
{
float cm;
float inches;
int operation;

printf("Hello and welcome to the Inches to Centimetres converter. \n");
printf("Choose from the options below by entering the corresponding number:\n");
printf("Inches to CM converter (1)\n");
printf("CM to Inches converter (2)\n");
scanf("&d", &operation);

if (operation == 1)
{
printf("Please enter the amount of Inches you wish to convert : \n");
scanf("%f", &inches);

cm = inches * 2.54;

if (inches <= 0)
{
printf("Invalid number");
}
else
printf("%f inches is equal to %f centimetres.", inches, cm);
}
else if (operation == 2);
{
printf("Please enter the amount of Centimetres you wish to convert : ");
scanf("%f", &cm);

inches = cm / 2.54;

if (cm <= 0)
{
printf("Invalid number");
}
else
printf("%f centimetres is equal to %f inches.", cm, inches);
}

}
``````

## 1 个回复1

### #1楼 票数：2 已采纳

``````scanf("&d", &operation);
``````

``````scanf("%d", &operation);
``````

``````}
else if (operation == 2);
{
``````

`;` 立即结束`else`块。 因此，大括号中的块将始终运行。 摆脱`;`

``````}
else if (operation == 2)
{
``````

``````} else if (operation == 2) {
``````

1回复

2回复

3回复

3回复

2回复

3回复

5回复

4回复