否则如果声明在c#中

时间:2017-04-29 18:18:40

标签: c#

我从' if'开始声明然后继续使用'否则如果',然后另一个'否则如果'。新课程开始时就是这样。第一个开始时是If,然后是第二个和第三个,否则if。

if语句,else语句和if else语句之间有什么区别?我完全糊涂了。任何澄清都会非常有帮助

 **if (selectedClass == 1)**
            {
                if (i >= 5)
                {

                    if (i >= 5 && j >= 10)
                    {
                        Console.WriteLine("\nI am sorry, there are no more seats on the flight");
                        break;

                    }

                    Console.WriteLine("\nThere are no first class seats available. Would you like an economy class seat? Type y for yes and n to exit.");
                    selectedClass = Convert.ToChar(Console.ReadLine());


                    if (selectedClass == 'y' || selectedClass == 'Y')
                    {
                        reserveEconomySeat(ref seats, ref j);
                    }

                    else 
                    {
                        Console.WriteLine("\nThe next flight leaves in 3 hours.");
                        break;
                    }
                }

                else reserveFirstSeat(ref seats, ref i); 
            }


            **else if (selectedClass == 2)**
            {
                if (j >= 10)
                {

                    if (i >= 5 && j >= 10)
                    {
                        Console.WriteLine("\nI am sorry, there are no more seats on the flight");
                        break;

                    }

                    Console.WriteLine("\nThere are no economy seats available. Would you like a first class seat? Type y for yes and n to exit.");
                    selectedClass = Convert.ToChar(Console.ReadLine());

                    if (selectedClass == 'y' || selectedClass == 'Y')
                    {
                        reserveFirstSeat(ref seats, ref i);
                    }

                    else
                    {
                        Console.WriteLine("\nThe next flight leaves in 3 hours.");
                        break;
                    }
                }

                else reserveEconomySeat(ref seats, ref j);
            }


            **else if (selectedClass == 0)**
            {
                Console.WriteLine("\nNext flight leaves in 3 hours.");
                break;
            }


            else if (selectedClass == 3)
            {
                DisplaySeatingChart(ref seats);
            }


            else
            {
                Console.WriteLine("\nInvalid entry. Please try again");
            }
        }

3 个答案:

答案 0 :(得分:1)

如果条件为真,

If语句只是执行后面的代码:

if (condition)
{
    //This code gets executed when the condition is true
}

Else(你称之为If else)只在之后的if(else)语句没有执行时才执行代码:

if(condition)
{
    //Gets executed if condition is true
}
else
{
    //Gets executed if condition is false
}
如果之前的if(else)语句没有执行并且条件为真,则

Else if执行代码:

if(condition1)
{
    //Gets executed if condition1 is true
}
else if(condition2)
{
    //Gets executed if condition1 is false and condition2 is true
}

有关详情,请阅读this

答案 1 :(得分:1)

看起来你是C#的新手。 1.

 if(condition)
 {
 //block
 }

在这种情况下,如果条件成立,则将执行块。

  1. if(condition)
    {
     //block1
    }
    else
    {
     //block2
    }
    
  2. 如果条件成立,则block1将执行else block2

    1. 第三种情况将是if else ladder。
    2. 您可以开始观看Youtube上的视频系列,Pluralsight。并且不要直接跳到C#上。从C语言入手,开发逻辑构建技能

答案 2 :(得分:0)

正如其他人提到的那样,

大括号内的代码" {}"在"下面"如果"将在" if(x)"条件是真的。

" else"如果条件为假或未满足,则简单地包含将执行的内容。

"否则如果"如果你想要两个以上的条件,可以使用它。

例如,

if(x > 5 && <= 10){
//execute
}else if (x > 11 && <= 20){
//execute
}else{
//execute

}

希望这有帮助