简体   繁体   中英

C# program for odd even number

Hi i am doing very basic c# coding where just checking odd and even number however how i can re-enter input while i am converting a string into integer at one place.

        if (i % 2==0) 

            {
            Console.WriteLine("even");
            Console.WriteLine("enter again", i);
        }
        else if(i%2!=0)

        {
            Console.WriteLine("odd");
            Console.WriteLine("enter again", i);
enter code here
        }

        Console.ReadKey();

You want to do something like that ?

static void Main(string[] args)
    {
        var l = string.Empty;
        while (l != "exit")
        {
            l = Console.ReadLine();
            int i;
            if (!int.TryParse(l, out i)) continue;
            Console.WriteLine(i%2 == 0 ? "even" : "odd");
            Console.WriteLine("enter again");
        }
        Console.ReadLine();
    }

Basically you need a loop and you could make it stop when the user enters a non-integer value.

int i;
Console.WriteLine("enter a number");
while(int.TryParse(Console.ReadLine(), out i))
{
    Console.WriteLine(i%2 == 0 ? "even" : "odd");
    Console.WriteLine("enter again");
}
static void Main()
{
    string userChoice,number;
    int checkInt;
    Console.WriteLine("Do you want check even/odd number?y/n");
    userChoice = Console.ReadLine();
    if (userChoice.ToLower().Equals("y"))
    {
        do
        {

            Console.WriteLine("Please enter your number");
            number = Console.ReadLine();
            if (int.TryParse(number, out checkInt))
            {
                if ((checkInt % 2) == 0)
                {
                    Console.WriteLine("Your entered number {0} is even", checkInt);
                }
                else
                {
                    Console.WriteLine("Your entered number {0} is odd", checkInt);
                }
            }
            else
            {
                Console.WriteLine("Plesae enter integer value");
            }

            Console.WriteLine("Do you want check even/odd number?y/n");
            userChoice = Console.ReadLine();
        } while (userChoice.ToLower().Equals("y"));
    }
}

I suppose you need a loop that ask you to insert a new number until it is convertible into a integer number.

I suggest you this possible solution

    static int ReadInput(string message)
    {
        int n = 0;
        do
        {
            Console.WriteLine(message);
        }
        while (!int.TryParse(Console.ReadLine(), out n));

        return n;
    }


    static void Main(string[] args)
    {
        int i = ReadInput("Enter a Number");
        if (i % 2==0) 
        {
            Console.WriteLine("even");
        }
        else if(i%2!=0)
        {
            Console.WriteLine("odd");
            //enter code here
        }
    }
}

In order to convert String into int for a even odd program, the following code can work out -

static void Main(string[] args)
{
        int i;
        Console.Write("Enter a Number : ");
        i = int.Parse(Console.ReadLine());

        if (i % 2 == 0)
        {
            Console.Write("Entered Number is an Even Number");
            Console.Read();
        }
        else
        {
            Console.Write("Entered Number is an Odd Number");
            Console.Read();
        }
}

What you want is a really fast method:

If (rowNum % 2 == 0)
Print even row color info

Else Print odd row color info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM