C# problem NaN

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            double x;

            try

            {


      x = double.Parse(Console.ReadLine(), System.Globalization.CultureInfo.InvariantCulture);

      double pierwiastek = Math.Sqrt(x);


                Console.WriteLine(pierwiastek.ToString("F3", System.Globalization.CultureInfo.InvariantCulture));


                if (x < 0)

                    Console.WriteLine("Liczba jest ujemna"); //Gdy x < 0 to liczba jest ujemna




            }

            catch (Exception)

            {

                Console.WriteLine("Blad danych"); //Gdy x to nie liczba

                return;

            }


        }





    }

}

gdy wpisuje liczby ujemne wyskakuje mi:

NaN

Liczba jest ujemna

i mam problem bo nie wiem jak usunąc to “NaN”

prosze o pomoc

Jest tak bo wyświetlasz pierwiastek nawet gdy jest to liczba ujemna. Wystarczy byś wyświetlenie pierwiastka dać po if dodając przed nim else :slight_smile:

Dodane 25.10.2011 (Wt) 19:58

aha, dzieki :slight_smile: