mardi 22 décembre 2015

Why does parsing a double from a string and then comparing it produce an unexpected result? [on hold]

I am having trouble with my if statement in c#. When I run the program and I enter a value higher than the maximum of height for example and a suitable one for width it maximizes both of them and give me an area of 100.

class Program
    {
        static void Main(string[] args) {

            const double MAX_HEIGHT = 10.0;
            const double MIN_HEIGHT = 1.0;
            const double MAX_WIDTH = 10.0;
            const double MIN_WIDTH = 1.0;

            double width, height, area;
            string widthString, heightString;

            Console.WriteLine("Rectangle: ");

            Console.WriteLine("Enter width: ");           
            widthString = Console.ReadLine();
            width = double.Parse(widthString);

            if (width < MIN_WIDTH)
            {
                Console.WriteLine("Width too small, using minimum.");
                width = MIN_WIDTH;
            }

            if (width > MAX_WIDTH)
            {
                Console.WriteLine("Width too big, using minimum.");
                width = MAX_WIDTH;
            }

            Console.WriteLine("Ënter height: ");
            heightString = Console.ReadLine();
            height = double.Parse(widthString);

            if (height < MIN_HEIGHT)
            {
                Console.WriteLine("Height too small, using minimum.");
                height = MIN_HEIGHT;
            }

            if (height > MAX_HEIGHT)
            {
                Console.WriteLine("Height too big, using minimum.");
                height = MAX_HEIGHT;
            }

            area = height * width;
            Console.WriteLine("The area is: " + area);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire