首页 > 解决方案 > 我如何制作 ac# 程序来知道一年是世界杯还是奥运会

问题描述

所以我想制作一个程序,它知道一年是世界杯还是奥运会,其他任何一年都会“一无所有”。而且我想让这些年在 2000-2100 之间。

Console.Write("Please enter a year between 2000-2100: ");

int year;
            Int32.TryParse(Console.ReadLine(), out year);
            Console.WriteLine("You wrote : " + year);

If (year%4 == 0 )
{
Console.WriteLine("World cup");
}
else if (year%4 == 0)
{
   Console.WriteLine("Olympic games");
}

我已经做到了这一点,但我无法让程序知道世界杯和奥运会之间的区别。


我给大家举个例子。我对编程很陌生。奥运会每 4 年举行一次,例如。2000, 2004, 2008, 2012, 2016 等等。如果年份能被 4 整除,那么它就是奥林匹克年。足球世界杯是两届奥运会之间的偶数年,例如。2002、2006、2010、2014、2018 等等。也就是说,如果这一年是偶数年而不是奥运年,那么它就是足球世界杯。每隔一年都会给出文本“今年没什么特别的”

像2003年的“今年没什么特别的” 2004年的“奥运年” 2006年的“世界杯”


using System;

namespace TEST 
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Write a year between 1950-2050: ");
            int year;
            Int32.TryParse(Console.ReadLine(), out year);
            Console.WriteLine("You wrote: " + year);
            if (year < 1950)
            {
                Console.WriteLine("1950-2050 not under");
            }

            else if (year > 2050)
            {
                Console.WriteLine("1950-2050 not over");
            }

            Console.ReadKey();
        }

这是到目前为止有效的代码。

标签: c#leap-year

解决方案


21 世纪的第一届 FIFA 世界杯是在 2002 年。您需要添加 2 才能获得余数。

if ((year+2)%4 == 0 )
{
Console.WriteLine("FIFA World Cup and Winter Olympics");
}

推荐阅读