Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我能';似乎无法从.txt文件中的不同元素中提取周长和面积方法_C# - Fatal编程技术网

C# 我能';似乎无法从.txt文件中的不同元素中提取周长和面积方法

C# 我能';似乎无法从.txt文件中的不同元素中提取周长和面积方法,c#,C#,我似乎无法获得矩形方法来提取文本文件中的第三个和第四个元素。我假设它从data.txt中提取不同的元素 矩形类 public Rectangle(double xCoord, double yCoord, double width, double height) { this.xCoord = xCoord; this.yCoord = yCoord; this.width = width; this.height = h

我似乎无法获得矩形方法来提取文本文件中的第三个和第四个元素。我假设它从data.txt中提取不同的元素

矩形类

public Rectangle(double xCoord, double yCoord, double width, double height) 
    {
        this.xCoord = xCoord;
        this.yCoord = yCoord;
        this.width = width;
        this.height = height;
    }

    public void SetWidth(double width)
    {
        this.width = width;
    }

    public void SetHeight(double height)
    {
        this.height = height;
    }

    public double GetWidth()
    {
        return width;
    }

    public double GetHeight()
    {
        return height;
    }

    public double GetPerimeter()
    {
        return 2* (width*height);

    }

    public double GetArea()
    {
        return width * height;
    }

    public override string DisplayString()
    {
        string output = string.Format("RECTANGLE:  X: {0} Y: {1} W: {2} H: {3} AREA: {4} PER: {4}"
                  , xCoord, yCoord, width, height, GetArea(), GetPerimeter()  );

        return output;
    }
文本文件

c|1.1|2.2|3.3
r|1.1|2.2|3.3|4.4
c|2.2|3.3|4.4
r|2.2|3.3|4.4|5.5
类读取TXT文件并调整显示

while (!sr.EndOfStream)
            {
                line = sr.ReadLine();
                var elements = line.Split('|');

                xcoord = Convert.ToDouble(elements[1]);
                ycoord = Convert.ToDouble(elements[2]);

                if (elements[0].ToLower().Equals("c"))
                {
                    radius = Convert.ToDouble(elements[3]);
                    tempshape = new Circle(xcoord, ycoord, radius);
                }

                if (elements[0].ToLower().Equals("r"))
                {

                    width = Convert.ToDouble(elements[3]);
                    height = Convert.ToDouble(elements[4]);

                    tempshape = new Rectangle(xcoord, ycoord, width, height);
                }

我认为另一个假设可能是问题出现在矩形类的if语句中的元素3和4中,但无法解决这个问题。

您正在为面积和周长打印相同的参数(
GetArea()
)。我想你是想让
PER:{5}

public覆盖字符串DisplayString()
{
字符串输出=string.Format(“矩形:X:{0}Y:{1}W:{2}H:{3}区域:{4}每:{5}”
,xCoord,yCoord,宽度,高度,GetArea(),getPermiture());
返回输出;
}

抱歉,我想这应该是一条注释,但我还不能发表一条注释。

您正在为面积和周长打印相同的参数(
GetArea()
)。我想你是想让
PER:{5}

public覆盖字符串DisplayString()
{
字符串输出=string.Format(“矩形:X:{0}Y:{1}W:{2}H:{3}区域:{4}每:{5}”
,xCoord,yCoord,宽度,高度,GetArea(),getPermiture());
返回输出;
}

PS:对不起,我想这应该是一条评论,但我还不能发布一条评论。

代码看起来不错。它编译吗?错误信息是什么?老实说,看起来没有什么不对的。。出了什么问题?你预计会发生什么?“我在假设…”,“另一个假设…”为什么?您是否设置了断点并运行了代码?是否完成了任何基本调试?@SQLHacks这样就不会出现错误消息,但当我在width元素上设置断点时,它默认为0,并且我完全不知道它为什么要这样做,即使我在txt中将它设置为3.3file@Matt所以它确实运行,这是好的,圆运行得很完美,但矩形给出了相同的答案周长和区域,这是不应该发生的。例如,第二行的面积和周长都是14.52,而不是面积:14.52和周长:29.04。代码看起来不错。它编译吗?错误信息是什么?老实说,看起来没有什么不对的。。出了什么问题?你预计会发生什么?“我在假设…”,“另一个假设…”为什么?您是否设置了断点并运行了代码?是否完成了任何基本调试?@SQLHacks这样就不会出现错误消息,但当我在width元素上设置断点时,它默认为0,并且我完全不知道它为什么要这样做,即使我在txt中将它设置为3.3file@Matt所以它确实运行,这是好的,圆运行得很完美,但矩形给出了相同的答案周长和区域,这是不应该发生的。例如,第二条线的面积和周长都是14.52,而不是面积:14.52和周长:29.04,这是很好的发现!这是一个答案,应该被标记为这样。好发现!这是一个答案,应该被标记为这样。