如果语句丢失(在函数main中。不能通过此错误

时间:2017-03-12 09:34:07

标签: c

#include <stdio.h>

int main()
{
int khm;

printf("Please enter how fast the vehicle was going in km//h: ");
scanf("%d%*c", &khm);

    if khm>60 khm<65
    {
        printf("Issue a warning: ");
    }   
    else

        if khm>65 khm<70    
        {
            printf("Issue a fine of $80: ");
        }   
        else

            if khm>70 khm<80
            {
                printf("Issue a fine of $150: ");
            }   
            else

                if khm>80
                {
                    printf("Issue a fine of $500: ");
                }   
                else
                {
                    printf("This vehicle is not speeding: ");
                }

        return(0);
}

3 个答案:

答案 0 :(得分:1)

 #include <stdio.h>

 int main()
{
 int khm;

 printf("Please enter how fast the vehicle was going in     km//h: ");
 // error  
 scanf("%d", &khm);
// this is how we should write..
if (khm>60 && khm<65)
{
    printf("Issue a warning: ");
}   
else

    if (khm>65 && khm<70)
    {
        ("Issue a fine of $80: ");
    }   
    else

        if( khm>70 &&  khm<80)
        {
            printf("Issue a fine of $150: ");
        }   
        else

            if( khm>80)
            {
                printf("Issue a fine of $500: ");
            }   
            else
            {
                printf("This vehicle is not speeding: ");
            }

    return(0);
 }

答案 1 :(得分:0)

除了你没有使用parantheses ()之外, 这是C:{/ p>中AND运算符的正确语法

if ((khm > 60) && (khm < 65))
{
   // code
}

答案 2 :(得分:0)

  • if khm>60 khm<65

  • if khm>65 khm<70

  • if khm>70 khm<80

  • if khm>80

这3个if语句在语法上是不正确的,原因有两个:

<强>首先

  

如果语句丢失(在函数main中

编译器在此错误消息中告诉您需要使用括号(如())来包含if语句的条件语句。通常,这也适用于while循环条件语句和for循环条件语句。

其次,在有多个条件的if语句中(如khm>70 khm<80),您需要在这两个条件之间指定逻辑AND或OR运算符。这两个条件告诉编译器是否要求满足条件以进入if语句的主体(这是通过AND运算符完成的),或者您只是希望满足其中一个条件(这是通过OR完成的)运营商)。

  • AND运算符具有以下符号:&&
  • OR运算符具有以下符号:||

根据您的情况判断,您需要&&运营商。

顺便说一下,你可以大大缩短你的代码,并且更容易阅读你把else和if语句放在同一行。而不是做:

else
    if (khm > 65 && khm < 70)   

你可以这样做:

else if (khm > 65 && khm < 70)   

因此,请将您的代码更改为:

if (khm > 60 && khm < 65)
    // Add your code here   

else if (khm > 65 && khm < 70)    
    // Add your code here

else if (khm > 70 && khm < 80)
    // Add your code here  

else if (khm > 80)
    // Add your code here   

else
    // Add your code here