【发布时间】:2017-10-01 08:57:32
【问题描述】:
我创建了一个简单的矩形类,我需要比较同一个矩形的两侧,但我不知道这怎么可能。在this.width下面的方法isSquare()中出现错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rectangle
{
class Rectangleclass
{
private String name;
private double length = 0.0;
private double width = 0.0;
public Rectangleclass(String n, double l, double w)
{
name = n;
length = l;
width = w;
}
public String Name() { return name; }
public double Length() { return length; }
public double Width() { return width; }
public double Area()
{
return length * width;
}
public bool IsSquare()
{
if (this.Width() = this.Length())
{
return true;
}
else
{
return false;
}
}
}
}
【问题讨论】:
-
if (this.Width() == this.Length()),总是使用双等号来表示相等。你也可以像这样缩短你的代码:return this.Width() == this.Length(); -
= 是赋值,== 是比较,比较值时必须使用双等号。
-
这里很容易知道错误是什么,但是以后,不要让我们自己假设或尝试弄清楚。如果您遇到错误,请尽可能明确。
-
有时阅读错误信息很有用
-
这个问题不应该发布。您的调试器应该准确地告诉您错误是什么。