C++ 如果/否则(初学者)

C++ 如果/否则(初学者),c++,if-statement,C++,If Statement,还有,我真的不知道如何解决这个问题。您可以在早期if的else部分中使用if表达式。此代码有三种不同的情况需要测试,看起来应该更像这样: printf("Sie duerfen Bier trinken \n"); if(xif/else语句的工作方式: if (x <= 15) { printf("Sie duerfen keinen Alkohol trinken! \n"); } else if (x < 18) { printf("

还有,我真的不知道如何解决这个问题。

您可以在早期
if
else
部分中使用
if
表达式。此代码有三种不同的情况需要测试,看起来应该更像这样:

printf("Sie duerfen Bier trinken \n");

if(xif/else
语句的工作方式:

if (x <= 15)
    {
    printf("Sie duerfen keinen Alkohol trinken! \n");
    }

else if (x < 18)
    {
    printf("Sie duerfen Bier trinken \n");
    }
else
    {
    printf("Sie duerfen Alkohol jeglicher Art trinken! \n");
    }
}
因此,在您的情况下,如果
x>=18

if (condition)
{
   //Perform this block of code
}
else
{
    //If the if block wasn't executed, run this block of code
}
if(x=18)
{
//此块也将被执行
}
}
if (condition)
{
   //Perform this block of code
}
else
{
    //If the if block wasn't executed, run this block of code
}
if (x<=15)
{
    //This won't get performed
}

else
{
    //This entire block will get performed, because the previous if block wasn't. 
    //So the first print statement in your else block will get executed. 
    if(x>=18)   
    {
        //This block will ALSO get performed
    }
}