Home > DeveloperSection > Forums > Write another function for this code using C#
E E Cummings
E E Cummings

Total Post:26

Points:182
Posted on    October-05-2013 9:20 AM

 C# C# 
Ratings:


 1 Reply(s)
 948  View(s)
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();
            }
        }       
    }
}


Dag Hammarskjold
Dag Hammarskjold

Total Post:24

Points:168
Posted on    October-05-2013 9:32 AM

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;
    }

Don't want to miss updates? Please click the below button!

Follow MindStick