📜  使用C#中的if-else和switch语句获取一个月中的总天数

📅  最后修改于: 2021-05-29 15:53:27             🧑  作者: Mango

给定数字N ,任务是编写一个C#程序以打印N月的天数。

例子:

可以使用以下方法完成此任务:

1.使用if else语句 检查适当的月数,然后使用else if语句打印一个月中的天数。天数如下:

Month = [1, 3, 5, 7, 8, 10, 12] , Number of days = 31
Month = [2] , Number of days = 28/29
Month = [4, 6, 9, 11] , Number of days = 30

下面是上述方法的实现:

输出:

C#
// C# Program to Print Number of Days
// in a Month using Else If Statement
  
using System;
  
public class GFG{
  
    // Function to print the Number
    // of Days in a N
    static void PrintNumberofDays(int N) 
    {
       // Check for the Ns
        if (N == 1 || N == 3 || N == 5 || N == 7
          || N == 8 || N == 10 || N == 12) {
              Console.Write("31");
        } 
          
        else if (N == 4 || N == 6 || N == 9 ||
               N == 11) {
               Console.Write("30");
        } 
          
        else if (N == 2) {
               Console.Write("28/29");
        }
    }
      
    // Driver Code
    static public void Main ()
    {
        int N = 5; // 1 <= N <= 12
        PrintNumberofDays(N);
    }
}


C#
// C# Program to Print Number of Days
// in a Month using Switch Condition
  
using System;
  
public class GFG{
  
    // Function to print the Number
    // of Days in a N
    static void PrintNumberofDays(int N) 
    {
        // Check for the N
        switch(N )
        {
            case 1:
            case 3:
            case 5:     
            case 7:
            case 8:
            case 10:
            case 12:             
                Console.Write("31");
                break;
              
            case 4: 
            case 6:
            case 9:
            case 11:                 
                Console.Write("30"); 
                break;
              
            case 2:
            Console.Write("28/29");
            break;
        }
    }
      
    // Driver Code
    static public void Main ()
    {
        int N = 5; // 1 <= N <= 12
        PrintNumberofDays(N);
    }
}


输出:

31

2.使用切换条件与其他条件相同,如果选中,在此检查月份号并将其重定向到个案,然后打印一个月中的天数。下面是上述方法的实现:

C#

// C# Program to Print Number of Days
// in a Month using Switch Condition
  
using System;
  
public class GFG{
  
    // Function to print the Number
    // of Days in a N
    static void PrintNumberofDays(int N) 
    {
        // Check for the N
        switch(N )
        {
            case 1:
            case 3:
            case 5:     
            case 7:
            case 8:
            case 10:
            case 12:             
                Console.Write("31");
                break;
              
            case 4: 
            case 6:
            case 9:
            case 11:                 
                Console.Write("30"); 
                break;
              
            case 2:
            Console.Write("28/29");
            break;
        }
    }
      
    // Driver Code
    static public void Main ()
    {
        int N = 5; // 1 <= N <= 12
        PrintNumberofDays(N);
    }
}

输出:

31