首页 > 解决方案 > 如何获得用户的输入并提供反馈

问题描述

我是编码的新手 - 刚刚开始。构建成功,但我想添加输入输出以获得消费者响应,以检查它是否真的有效。谢谢

using System;

namespace LeapYear
{
    public static class Leap
    {
        static void Main(string[] args)

        {
            Console.WriteLine("Enter a year ");
            Console.ReadLine();
        }

        public static bool IsLeapYear(int year)
            { 

            if (year % 4 == 0 && year % 100 != 0 || year % 400 ==0)

            {
                return true;
            }

            return false;
        }

    }
}

标签: c#inputoutputconsole-application

解决方案


干得好:

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Enter a year ");

        //Console.ReadLine returns the entered data as a string
        var yearString = Console.ReadLine();

        //Convert the string to an int
        int year = Int32.Parse(yearString);

        //Now we can call your function, passing the year variable and recording the bool value passed back
        var isLeapYearAnswer = IsLeapYear(year);

        //Print the answer to the console
        Console.WriteLine(isLeapYearAnswer);
    }

    public static bool IsLeapYear(int year)
    { 
        if (year % 4 == 0 && year % 100 != 0 || year % 400 ==0)
            {
                return true;
            }
        else
        {
            return false;
        }
    }
}

这是一个dotnetfiddle


推荐阅读