If-Else statements c#



我必须得到两个矩形的长度和宽度。然后确定它们是否具有相同的面积,或者矩形1是否大于或小于矩形2。当我测试这个代码时,无论我输入什么值,得到的响应都是区域在不应该的时候是相同的。

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#程序从上到下逐行执行。你的问题来了,因为你是在用户输入尺寸之前计算面积的。试试这个:

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

在从用户那里获得长度和宽度之前,您正在计算两个矩形的面积。矩形一和矩形二都等于0。

您进行调用的顺序导致area1和area2每次都为0。试试这个:

    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();

您需要将area1和area2的初始化放在getArea1()/getArea2()调用之后,以便length、width、lengthTwo和widthTwo具有值。

此外,我建议您在调用greaterArea()后放一个Console.ReadKey(),这样控制台就不会立即关闭,您可以阅读消息。

最新更新