Home > Mobile >  if statement in public void won't run
if statement in public void won't run

Time:12-01

It maybe a very stupid question, but my public void with if statement doesn't run and I just can't find the answer why. I tried debugging, looking for similar topics in the web, but nothing helped. The funny thing in a video course which I am currently studying where a similar task is the codes runs although there are some differences which aren't relevant.

public class Program
{
    static int InputYear;
    static int YearDiv2;
    static int YearDiv4;
    static int YearDiv100;
    static int YearDiv400;
    string Yes = "Yes";
    string No = "No";
    string DivBy = "Divisible by ";
    string LeapYear = "Leap Year: ";


    public static void Main(string[] args)
    {
        //Section 5 Task

        Console.WriteLine("Please enter a year value:");
        InputYear = Convert.ToInt32(Console.ReadLine());
        YearDiv2 = InputYear % 2;
        //Console.WriteLine(YearDiv2);
        YearDiv4 = InputYear % 4;
        YearDiv100 = InputYear % 100;
        YearDiv400 = InputYear % 400;

        Console.WriteLine($"Year entered: {InputYear}");

    }
    public void YearNotOdd()
    {
        Console.WriteLine("It runs");
        if (InputYear == 0)
        {
            
            if (YearDiv4 == 0 && YearDiv100 == 0 && YearDiv400 == 0)
            {
                Console.WriteLine(@"{DivBy} 4: {Yes}");
                Console.WriteLine(@"{DivBy} 100: {Yes}");
                Console.WriteLine(@"{DivBy} 400: {Yes}");
                Console.WriteLine(LeapYear   Yes);
            }

            else if(YearDiv4 == 0 && YearDiv100 == 0 && YearDiv400 != 0)
            {
                Console.WriteLine(@"{DivBy} 4: {Yes}");
                Console.WriteLine(@"{DivBy} 100: {Yes}");
                Console.WriteLine(@"{DivBy} 400: {No}");
                Console.WriteLine(LeapYear   No);
            }

            else
            {
                Console.WriteLine(@"{DivBy} 4: {No}");
                Console.WriteLine(@"{DivBy} 100: {No}");
                Console.WriteLine(@"{DivBy} 400: {No}");
                Console.WriteLine(LeapYear   No);
            }


        }
        else
        {
            Console.WriteLine(@"{DivBy} 4: {No}");
            Console.WriteLine(@"{DivBy} 100: {No}");
            Console.WriteLine(@"{DivBy} 400: {No}");
            Console.WriteLine(LeapYear   No);
        }
    }
}

CodePudding user response:

You'll need to use the method YearNotOdd() somewhere, if you create a new method, then it needs to be called somewhere to be used.

To try it out, you should put the YearNotOdd() in your Main() method.

For example:

public static void Main(string[] args)
{
    //Section 5 Task

    Console.WriteLine("Please enter a year value:");
    InputYear = Convert.ToInt32(Console.ReadLine());
    YearDiv2 = InputYear % 2;
    //Console.WriteLine(YearDiv2);
    YearDiv4 = InputYear % 4;
    YearDiv100 = InputYear % 100;
    YearDiv400 = InputYear % 400;

    Console.WriteLine($"Year entered: {InputYear}");

    YearNotOdd(); //place it here
}

You might need to add static to the YearNotOdd() for this.

  • Related