c#逻辑不起作用

本文关键字:不起作用 | 更新日期: 2023-09-27 18:16:19

我是c#新手,我的逻辑行不通它一直显示我的else命令没有错误

///////////////////////////////////////////////////////////////////
        int age = 12;
        if ((age <= 0) && (age >= 12))
        {
            Console.WriteLine("You are young");
        }
        else if ((age <= 13) && (age >= 17))
        {
            Console.WriteLine("You're a teen");
        }
        else if ((age <= 18) && (age >= 50))
        {
            Console.WriteLine("You're an adult");
        }
        else if ((age <= 51) && (age >= 120))
        {
            Console.WriteLine("You're Elderly");
        }else
        {
            Console.Beep();
        }
        ///////////////////////////////////////////////////////////////////

c#逻辑不起作用

你只需要把你的条件换成:

int age = 12;
if ((age >= 0) && (age <= 12))
{
    Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
    Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
    Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
    Console.WriteLine("You're Elderly");
}
else
{
    Console.Beep();
}
int age = 12;
if ((age >= 0) && (age <= 12))
{
    Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
    Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
    Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
    Console.WriteLine("You're Elderly");
}else
{
    Console.Beep();
}
    语法没有问题,只是你的逻辑有问题。
  1. 查看所有<=条件。

if语句中的条件不正确,请尝试:

int age = 12;
if ((age >= 0) && (age <= 12))
{
    Console.WriteLine("You are young");
}
else if ((age >= 13) && (age <= 17))
{
    Console.WriteLine("You're a teen");
}
else if ((age >= 18) && (age <= 50))
{
    Console.WriteLine("You're an adult");
}
else if ((age >= 51) && (age <= 120))
{
    Console.WriteLine("You're Elderly");
}
else
{
    Console.Beep();
}