首页 > 解决方案 > 为什么阴影区域与矩形区域相同?

问题描述

这是我的第一篇文章,所以请多多包涵。我是 C# 的超级新手。我正在尝试以下操作:编写一个控制台应用程序,允许用户输入计算矩形面积、圆形面积所需的尺寸,然后显示下面阴影形状的面积。[1][1]:https://i.stack.imgur.com/u0kkN.png

这是我到目前为止所得到的: 我的问题:它运行良好,虽然问题是当我运行程序时,唯一错误的是它计算的阴影区域值。它似乎将矩形的区域显示为阴影区域的答案。因此,如果我为矩形输入 10 个长度和 10 个宽度。阴影区域和面积一样将是 100。我在我的代码中看不到任何问题,我已经尝试了我能想到的一切。将不胜感激任何帮助。谢谢 <3

**Program.cs**

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

namespace Section_A_Excercise_2
{
    class Program
    {
        static void Main(string[] args)
        {
            clsCalculate Calculate = new clsCalculate();
            Calculate.CalculateRectangle();
            Calculate.CalculateCircle();
            Calculate.CalculateShaded();
            Console.ReadLine();            
        }
    }
}








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

namespace Section_A_Excercise_2
{
    class clsCalculate : clsVariables
    {
        public void CalculateRectangle()
        {
            string title = @"
               __        __   _                            _   _               _ 
               \ \      / /__| | ___ ___  _ __ ___   ___  | | | |___  ___ _ __| |
                \ \ /\ / / _ \ |/ __/ _ \| '_ ` _ \ / _ \ | | | / __|/ _ \ '__| |
                 \ V  V /  __/ | (_| (_) | | | | | |  __/ | |_| \__ \  __/ |  |_|
                  \_/\_/ \___|_|\___\___/|_| |_| |_|\___|  \___/|___/\___|_|  (_)";

            Console.WriteLine(title);

            Console.WriteLine();
            Console.WriteLine("To Calculate the Area of a Rectangle:");
            Console.WriteLine("Please enter a length: ");
            Length = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Please enter a width: ");
            Width = Convert.ToInt32(Console.ReadLine());

            AreaofRectangle = Length * Width;
            Console.WriteLine("The area of the rectangle of dimensions " + Length + " by " + Width + " is: " + AreaofRectangle);

        }

        public void CalculateCircle()
        {
            Console.WriteLine();
            Console.WriteLine("To Calculate the Area of a Circle:");
            Console.WriteLine("Please enter a radius: ");
            Radius = Convert.ToInt32(Console.ReadLine());
            double AreaofCircle;
            AreaofCircle = (3.14) * Radius * Radius;
            Console.WriteLine("The area of the circle with radius " + Radius + " is: " + AreaofCircle);
        }

        public void CalculateShaded()
        {
            Console.WriteLine();       
            Console.WriteLine("To Calculate the Shaded Area (Area of Rectangle Minus Area of 
            Circle):");
            ShadedArea = AreaofRectangle - AreaofCircle; 
            Console.WriteLine("The shaded area is equal to: " + ShadedArea);
        }

    }
}















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

namespace Section_A_Excercise_2
{
    class clsVariables
    {
        protected int Length;
        protected int Width;
        protected int Radius;
        protected int AreaofRectangle;
        protected int AreaofCircle;
        protected int ShadedArea;

        public int _Width
        {
            get { return Width; }
            set { Width = value; }
        }
        public int _Length
        {
            get { return Length; }
            set { Length = value; }
        }
        public int _Radius
        {
            get { return Radius; }
            set { Radius = value; }
        }
        public int _AreaofRectangle
        {
            get { return AreaofRectangle; }
            set { AreaofRectangle = value; }
        }
        public int _AreaofCircle
        {
            get { return AreaofCircle; }
            set { AreaofCircle = value; }
        }

        public int _ShadedArea
        {
            get { return ShadedArea; }
            set { ShadedArea = value; }
        }

    }
}

标签: c#

解决方案


我解决了!谢谢您的帮助!基本上,问题是因为我的方法括号中有“双圆区域”、“双矩形区域”,所以它每次都定义一个新变量,而不是从我的变量类中获取它。所以我删除了那些,它起作用了!再次感谢你!


推荐阅读