如何从我的属性中获取两个值以返回到我的main方法?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从我的属性中获取两个值以返回到我的main方法?相关的知识,希望对你有一定的参考价值。
以下代码应返回2个值,这些值是属性WallArea
和GallonsOfPaint
中的字段。这应该返回到main方法。 Room
类包含2个私有方法CalcWallArea
和CalcAmountOfPaint
,它们将设置我刚刚提到的两个属性的值。
问题是它只会返回另一个。我不能让它返回两者。结果应该是用户输入长度。宽度和高度方法将告诉房间的平方英尺,并告诉我们需要多少加仑的油漆来绘制房间。目前在跑的时候它只会告诉平方镜头,或者如果我打电话给第二种方法,那么它会告诉多少加仑的油漆,但它不能同时做到。有人可以帮忙吗?
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分配给临时变量并从中减去。
尽管如此,最好还是要放弃对这些属性和方法的调用如何影响对象的状态。
为此,我会相应调整您的Room类:
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;
}
}
以上是关于如何从我的属性中获取两个值以返回到我的main方法?的主要内容,如果未能解决你的问题,请参考以下文章
Haskell Persistent Library - 如何从我的数据库中获取数据到我的前端?
如何将 2 个属性从我的 App Store 版本交换到我当前的开发版本?
如何将消息从我的存储库类返回到我的控制器,然后返回到我在 asp.net-mvc 中的视图?
如何使用 promise 将数组从我的模块返回到我的 API?异步函数和承诺 - Node.js