WRITE ANOTHER FUNCTION FOR THIS CODE USING C#

E E Cummings

Total Post:26

Points:182
Posted by  E E Cummings
C# 
C#
 1279  View(s)
Ratings:
Rate this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string numberIn;
            int numberOut;
 
            numberIn = Console.ReadLine();
 
            while (!int.TryParse(numberIn, out numberOut) || numberOut < 0)
            {
                Console.WriteLine("Invalid. Enter a number that's 0 or higher.");
                numberIn = Console.ReadLine();
            }
        }       
    }
}
  1. Dag Hammarskjold

    Post:24

    Points:168
    Re: Write another function for this code using C#

    static void Main(string[] args)
        {
            string numberIn = Console.ReadLine();
            int numberOut;
            while(!IsNumeric(numberIn))
            {
                Console.WriteLine("Invalid.  Enter a number that's 0 or higher.");
                numberIn = Console.ReadLine();
            }
            numberOut = int.Parse(numberIn);
        }
        private static bool IsNumeric(string num)
        {
            return num.ToCharArray().Where(x => !Char.IsDigit(x)).Count() == 0;
        }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!