C#将列表对象与传递的参数进行比较

C#将列表对象与传递的参数进行比较,c#,list,c#-4.0,C#,List,C# 4.0,我想做的是得到两边1到20之间的直角三角形的数目 大多数逻辑都很好,但当我想检查3,4和5时,这是一个三角形,而4,3和5将无效,因为它是以不同的顺序排列的3,4,5 下面是我为问题区域编写的代码 public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList) { bool breakLoop = false; Int32 length = triangle

我想做的是得到两边1到20之间的直角三角形的数目

大多数逻辑都很好,但当我想检查3,4和5时,这是一个三角形,而4,3和5将无效,因为它是以不同的顺序排列的3,4,5

下面是我为问题区域编写的代码

public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList)
{
    bool breakLoop = false;
    Int32 length = triangleList.Count;
    for (int index = 0; index < length && breakLoop != false; index++)
    {
        //This is to compare an existing adjacent that is stored in the list to the
        //supplied opposite, this is to prebent the 3, 4, 5 and 4, 3, 5 issue
        var response = triangleList.Find(r => r.IntAdjacent == intOpp);

        if (response !=null)
        {
            //This is to compare an existing opposite that is stored in the list to the
            //supplied adjacent, this is to prebent the 3, 4, 5 and 4, 3, 5 issue
            var otherResponse = triangleList.Find(r => r.IntOpposite == intAdj);

            if (otherResponse != null)
            {
                breakLoop = true;
            }
        }
    }
    return breakLoop;
}
有没有人能看到我在逻辑上犯了什么错误,或者代码本身犯了什么错误?
基思

您可以将其简化为:

public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList)
{
    if(triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp))
        return true;

    return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj);

}
public static bool isAlreadyValidTriangle(this List<Triangle> triangleList, int intAdj, int intOpp)
{
    if(triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp))
        return true;

    return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj);

}
这意味着您可以将其称为更具可读性的:

List<Triangle> triangleList = new List<Triangle>();
... fill list with triangles ...

if(triangleList.isAlreadyValidTriangle(adjacent, opposite)
{
    ...
}
List triangleList=new List();
... 用三角形填充列表。。。
if(三角形列表。isAlreadyValidTriangle(相邻、相反)
{
...
}

首先感谢您的建议和帮助

下面是从头到尾的完整代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;

    namespace ConsoleApplication1
    {
    public class Program
    {
    private static double doubleHypotenuse = 0;
    private static int adjacent = 1;
    private static int opposite = 1;
    private static int limit = 200;
    private static int count = 0;

    public static void Main(string[] args)
    {

        TriangleLogic triLogic = new TriangleLogic();
        List<Triangle> triangleList = new List<Triangle>();
        List<Triangle> trianglePlus1 = new List<Triangle>();

        while (adjacent < limit)
        {
            opposite = 1;
            while (opposite < limit)
            {
                doubleHypotenuse = triLogic.intRightAngle(adjacent, opposite);
                if (doubleHypotenuse % 1 == 0)
                {
                    if (!triLogic.isAlreadyValidTriangle(adjacent, opposite, triangleList))
                    {
                        triangleList.Add(new Triangle(adjacent, opposite, (int)Convert.ToInt32(doubleHypotenuse)));
                    }

                    count++;
                }
                opposite++;
            }
            adjacent++;
        }

        Console.WriteLine("The following are integer triangles");
        triangleList.ForEach(delegate(Triangle pytag)
        {
            if ((pytag.IntHypotenuse - pytag.IntOpposite) == 1)
            {
                trianglePlus1.Add(new Triangle(pytag.IntAdjacent, pytag.IntOpposite, pytag.IntHypotenuse));
            }
            Console.WriteLine(pytag.IntAdjacent + ", " + pytag.IntOpposite + " and " + pytag.IntHypotenuse);
        });

        Console.WriteLine("the number of squares is " + count);
        Int32 length = triangleList.Count;
        Console.WriteLine("the length of the list is " + length);

        Console.WriteLine("");
        Console.WriteLine("the List of triangles with the hypotenuse 1 ");
        Console.WriteLine("more than the opposite");

        trianglePlus1.ForEach(delegate(Triangle pytagPlus1)
        {
            Console.WriteLine(pytagPlus1.IntAdjacent + ", " + pytagPlus1.IntOpposite + " and " + pytagPlus1.IntHypotenuse);
        });


        Int32 lengthPlus1 = trianglePlus1.Count;
        Console.WriteLine("the length of the list is " + lengthPlus1);

    }
}
}
最后是三角逻辑类

    public class TriangleLogic
    {
    private double squareAdjacent = 0;
    private double squareOpposite = 0;
    private double squareSum = 0;

    public TriangleLogic()
    {

    }

    public double intRightAngle(int intAdjacent, int intOpposite)
    {
        squareAdjacent = Math.Pow(Convert.ToDouble(intAdjacent), 2);
        squareOpposite = Math.Pow(Convert.ToDouble(intOpposite), 2);
        squareSum = squareAdjacent + squareOpposite;
        return Math.Sqrt(squareSum);
    }

    public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList)
    {
        if (triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp))
            return true;

        return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj);
    }
}
公共类三角逻辑
{
私有双平方=0;
私有双平方=0;
私有双平方和=0;
公共三角逻辑()
{
}
公共双内直角(内不相邻,内不相对)
{
平方相邻=数学功率(转换为双倍(不相邻),2);
平方对立面=数学功率(转换为双倍(对立面),2);
平方和=平方相邻+平方相反;
返回数学Sqrt(平方和);
}
公共bool是一个alreadyvalidtriangle(int-intAdj、int-intOpp、List-triangleList)
{
if(triangleList.Any(t=>t.IntAdjacent==intAdj&&t.IntOpposite==intOpp))
返回true;
返回triangleList.Any(t=>t.IntAdjacent==intOpp和&t.IntOpposite==intAdj);
}
}

再次感谢支持

“我要做的是得到两边1到20之间直角三角形的数目”,读回自己,并认为三角形有三条边,这些都不能用“二者”一词来解决。。你是在寻找所有边都小于20的三角形吗?列表不是无限的吗?你能澄清一下你的目标是什么、你期望的结果是什么以及实际的问题是什么吗?由于你的方法有点错误,代码无法准确地工作。这么说是因为你正在搜索整个列表,其中可能包含许多ob对象中有大量的值,因此Find方法在找到它的任何地方返回一个值,而不是在特定的索引中。您必须比较列表中每个特定索引的值。intAdj&
intOpp
与triangleList的关系是什么?以及
for
循环的目的是什么?我看不到
>索引
在循环中使用。@当他说“两者”时,请挥霍,这是准确的,因为它们是直角三角形,所以斜边不会改变,只有相邻的和相反的那个开关。@EXCENTION我不明白你在说什么。我的代码不同,因为它同时比较相邻的和相反的。很抱歉误解了你的代码,没错,我没有注意到
&
我对我的答案进行了另一次编辑。我知道你已经接受了,但它一直困扰着我,我不得不做出更改:)
    public class Triangle
    {
    private int intAdjacent;
    private int intOpposite;
    private int intHypotenuse;

    public Triangle(int intAdjacent, int intOpposite, int intHypotenuse)
    {
        this.intAdjacent = intAdjacent;
        this.intOpposite = intOpposite;
        this.intHypotenuse = intHypotenuse;
    }

    public int IntAdjacent
    {
        get { return intAdjacent; }
    }

    public int IntOpposite
    {
        get { return intOpposite; }
    }

    public int IntHypotenuse
    {
        get { return intHypotenuse; }
    }
}
    public class TriangleLogic
    {
    private double squareAdjacent = 0;
    private double squareOpposite = 0;
    private double squareSum = 0;

    public TriangleLogic()
    {

    }

    public double intRightAngle(int intAdjacent, int intOpposite)
    {
        squareAdjacent = Math.Pow(Convert.ToDouble(intAdjacent), 2);
        squareOpposite = Math.Pow(Convert.ToDouble(intOpposite), 2);
        squareSum = squareAdjacent + squareOpposite;
        return Math.Sqrt(squareSum);
    }

    public bool isAlreadyValidTriangle(int intAdj, int intOpp, List<Triangle> triangleList)
    {
        if (triangleList.Any(t => t.IntAdjacent == intAdj && t.IntOpposite == intOpp))
            return true;

        return triangleList.Any(t => t.IntAdjacent == intOpp && t.IntOpposite == intAdj);
    }
}