簡體   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