繁体   English   中英

如何从属性中获取两个值以返回到主方法?

[英]How do I get both values from my property to be returned to my main method?

以下代码应返回2个值,这些值是属性WallAreaGallonsOfPaint中的字段。 这应该返回到main方法。 Room类包含2个私有方法CalcWallAreaCalcAmountOfPaint ,它们将为我刚刚提到的两个属性设置值。

问题是它只会返回另一个。 我不能让它同时返回。 结果应该是用户输入长度时。 宽度和高度的方法将告诉房间的平方英尺,并告诉您油漆房间需要多少加仑油漆。 当前,当它运行时,它只会告诉平方英尺或如果我调用第二种方法,那么它将告诉多少加仑油漆,但不会两者都做。 有人可以帮忙吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Array;

namespace PaintingRoomDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Room aRoom = new Room();
            string numberString;

            WriteLine("Please enter the length of the wall in feet");
            numberString = ReadLine();

            aRoom.Length = Convert.ToInt32(numberString);

            WriteLine("Please enter the width of the wall in feet");
            numberString = ReadLine();

            aRoom.Width = Convert.ToInt32(numberString);

            WriteLine("Please enter the height of the wall in feet");
            numberString = ReadLine();

            aRoom.Height = Convert.ToInt32(numberString);

            Write("The room area is: {0} and requires {1} gallons of paint",
                aRoom.WallArea, aRoom.GallonsOfPaint);
            ReadLine();
        }
    }
    class Room
    {
        private int wallArea; //These are data fields//
        private int numberOfGallonsOfPaintNeeded; //These are data fields//
        private int length; //These are data fields//
        private int width; //These are data fields//
        private int height; //These are data fields//
        private int total;
        public int Length //This is a property which provides access to the data field length
        {
            get {return length;}
            set {length = value;}
        }
        public int Width //This is a property which provides access to the data field width
        {
            get {return width;}
            set {width = value;}
        }
        public int Height //This is a property which provides access to the data field height
        {
            get {return height;}
            set { height = value; CalcWallArea();}
        }
        public int WallArea //This is a property that should return wallArea
        {
            get {return wallArea;}
        }
        public int GallonsOfPaint //This is a property that should return wallArea
        {
            get {return numberOfGallonsOfPaintNeeded;}
        }
        private void CalcWallArea() //This is a private method that needs to be called to add the value to CalcAmountOfPaint field
        {
            wallArea = (Length + Width + Length + Width) * Height;
            CalcAmountOfPaint();
        }
        private void CalcAmountOfPaint() //This is a private method that needs to be called to add the value to CalcAmountOfPaint field
        {
            if (wallArea <= 350)
                numberOfGallonsOfPaintNeeded = 1;

            int x = 1;
            if (wallArea >= 350)
                while (wallArea > 0)
                {
                    x++;
                    wallArea = wallArea - wallArea;
                    numberOfGallonsOfPaintNeeded = x;
                }
        }
    }
}

我建议您进行一些更改,以帮助您获得所需的行为。

首先,我建议不要在Property setter或getter中调用更改类状态的方法,因为这通常导致难以通过代码进行推理。

我还建议更改两个函数以返回所需的值,而不是让它们更改字段的状态,并让属性简单地返回这些值。

至于您眼前的问题,此问题在您的CalcGallonsOfPaint方法中。

while (wallArea > 0)
{
    x++;
    wallArea = wallArea - wallArea; // <- right here
    numberOfGallonsOfPaintNeeded = x;
}

计算的这一部分将始终将墙壁面积设置为0,因为它会从自身中减去其全部值。 我怀疑您的意思是减去350的值,但是您还要更改用于返回WallArea的字段值。 至少,您应该将wallArea分配给一个临时变量,然后从中减去。

尽管如此,最好还是消除对这些属性和方法的调用对对象状态的影响。

为此,我将相应地调整您的房间等级:

class Room
{
    public int Length {get;set;}
    public int Width {get;set;}
    public int Height {get;set;}
    public int WallArea
    {
        get {return CalcWallArea();}
    }
    public int GallonsOfPaint 
    {
        get {return CalcGallonsOfPaint();}
    }
    private int CalcWallArea()
    {
        // I am assuming this calculation is correct for your needs.
        return (Length + Width + Length + Width) * Height;
    }
    private int CalcAmountOfPaint() 
    {
        var area = WallArea;
        if (area <= 350)
            return 1;

        int x = 0;
        while (area > 0)
        {
            x++;
            area -= 350;
        }
        return x;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM