简体   繁体   中英

If-Else statements c#

I have to get the length and width for 2 rectangles. Than determine if they have the same area, of if rectangle 1 is greater than or less than rectangle 2. When I test this code, no matter what values I put in, the response is that the areas are the same when they shouldn't be.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace twoRectangles
{
     class Program
    {


    static void Main(string[] args)
    {//declare variables and methods
        double length = 0.00;
        double width = 0.00;
        double lengthTwo = 0.00;
        double widthTwo = 0.00;
        double area1 = length * width;
        double area2 = lengthTwo * widthTwo;
        getArea1(ref length, ref width);
        getArea2(ref lengthTwo, ref widthTwo);
        greaterArea(ref area1, ref area2);
    }//call method getArea1
    static void getArea1(ref double length, ref double width)
    {//input for length of first rectangle
        Console.WriteLine("Please enter the length of the first rectangle:");
        while (!double.TryParse(Console.ReadLine(), out length))
            Console.WriteLine("Error, please enter a valid number");
        //input for width of frist rectangle
        Console.WriteLine("lease enter the width of the first retangle:");
        while (!double.TryParse(Console.ReadLine(), out width))
            Console.WriteLine("Error, please enter a valid number");
    }//call method get Area2
    static void getArea2(ref double lengthTwo, ref double widthTwo)
    {//input for length of second rectangle
        Console.WriteLine("Please enter the length of the second rectangle:");
        while (!double.TryParse(Console.ReadLine(), out lengthTwo))
            Console.WriteLine("Error, please enter a valid number");
        //input for width of second rectangle
        Console.WriteLine("Please enter the width of the second rectangle:");
        while (!double.TryParse(Console.ReadLine(), out widthTwo))
            Console.WriteLine("Error, please enter a valid number");

    }//call method greaterArea
    static void greaterArea(ref double area1, ref double area2)
    {//if statements
        if (area1 == area2)
        {
            Console.WriteLine("The areas of the rectangles are the same");
        }
        else if(area1 > area2)
        {
            Console.WriteLine("The area of Rectangle 1 is greater than Rectangle 2");
        }
        else if(area1 < area2)
        {
            Console.WriteLine("The area of Rectangle 1 is less than Rectangle 2");
        }


    }
}

C# programs are executed, line by line from the top to the bottom. Your problem is coming because you are calculating the area before the user has input the dimensions. try this:

    getArea1(ref length, ref width);
    getArea2(ref lengthTwo, ref widthTwo);
    double area1 = length * width;
    double area2 = lengthTwo * widthTwo;
    greaterArea(ref area1, ref area2);

You are calculating the area of the two rectangles before getting the length and width from the user. Rectangle one and two both are equivalent to 0.

The order you are making the calls is resulting in area1 and area2 being 0 everytime. Try this:

    double length = 0.00;
    double width = 0.00;
    double lengthTwo = 0.00;
    double widthTwo = 0.00;
    getArea1(ref length, ref width);
    getArea2(ref lengthTwo, ref widthTwo);
    double area1 = length * width;
    double area2 = lengthTwo * widthTwo;
    greaterArea(ref area1, ref area2);
    Console.ReadKey();

You need to put initialization of area1 and area2 after the getArea1() / getArea2() calls so that length, width, lengthTwo, and widthTwo have values.

Also, I recommend you put a Console.ReadKey() after your call to greaterArea() so that the console doesn't immediately close and you can read the message.

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