我如何比较一个数组中的数字与另一个数组中的数字并找出差异?我正在使用 C#

how do i compare a number in one array with a number in another array and find the difference? im using C#

我遇到了以下问题,我不知道如何比较两个数组并显示两者之间的差异或表明它是平局。请帮忙做这个练习。我目前拥有的代码不允许我找出每辆车的不同之处。我不确定我必须做什么。

雪佛兰和福特每队各有八辆汽车。每支车队的一辆汽车在阻力带上与对手比赛。阅读八辆雪佛兰汽车的比赛时间,然后阅读八辆福特汽车的比赛时间。将时间存储到名为 Chevy[ ] 和 Ford[ ] 的数组中。然后列出每对的获胜者,给出获胜者获胜的秒数。最后根据哪支球队获胜最多宣布哪支球队获胜。下面是一个示例匹配。

输入雪佛兰汽车的时间:5.4 7.2 4.0 9.1 5.8 3.9 6.2 8.1 输入相应福特汽车的时间:5.8 6.9 3.9 9.2 5.8 3.8 6.0 8.5 获胜者是: 雪佛兰 0.4 秒 福特领先 0.3 秒 福特领先 0.1 秒 雪佛兰 0.1 秒 领带 ! 福特领先 0.1 秒 福特领先 0.2 秒 雪佛兰 0.4 秒 获胜队伍是:F O R D !

• 将每辆 Chevy 汽车的比赛时间接受到数组 Chevy[ ]。
• 接受每辆福特汽车的比赛时间到数组 Ford[ ].
• 然后宣布每场比赛的获胜车辆,以秒为单位给出获胜时间。 • 如果时间相同,则宣布比赛平局。 • 最后,宣布哪支球队赢得了比赛,假设平局是可能的。

这是我的代码

    {

        //declare varibles
        double[] chevy = new double[8];
        double[] ford = new double[8];
        int x, y;
        double ctotal = 0, chevyaverage = 0;
        double ftotal = 0, fordaverage = 0;
        double cwin= 0, fwin = 0;
        //calculations

        //input
        for (x = 0; x < 8; x++)
        {
            Console.Write("Enter time for chevy car " + (x + 1) + ":");
            chevy[x] = double.Parse(Console.ReadLine());
        }
        for (y = 0; y < 8; y++)
        {
            Console.Write("Enter time for ford car " + (y + 1) + ":");
            ford[y] = double.Parse(Console.ReadLine());
        }
                         //this is not working, it keeps coming up as 1
        if (chevy[x] < ford[y])
        {
            cwin = chevy[x] - ford[y];
            Console.WriteLine("Chevy won by: " + cwin);
        }
        else if (ford[y] < chevy[x])
        {
            fwin = ford[y] - chevy[x];
            Console.WriteLine("Ford won by: " + fwin);
        }
        else
        {
            Console.WriteLine("Tie!");
        }

        //output


        Console.ReadLine();

   }
}

试试这个,你已经写了大部分问题,你添加了变量cn和fn,公司的获奖汽车数量,最后比较它们

{

    //declare varibles
    double[] chevy = new double[8];
    double[] ford = new double[8];
    int x, y;
    double ctotal = 0, chevyaverage = 0;
    double ftotal = 0, fordaverage = 0;
    double cwin= 0, fwin = 0;
    //calculations

    //input
    for (x = 0; x < 8; x++)
    {
        Console.Write("Enter time for chevy car " + (x + 1) + ":");
        chevy[x] = double.Parse(Console.ReadLine());
    }
    for (y = 0; y < 8; y++)
    {
        Console.Write("Enter time for ford car " + (y + 1) + ":");
        ford[y] = double.Parse(Console.ReadLine());
    }
    int cn=0,fn=0; //count of chevy wins and ford wins
    for(int x=0,int y=0;x<8;x++,y++)
    {

      if (chevy[x] < ford[y])
      {
        cn++;//chevy wins soo increment this
        cwin = chevy[x] - ford[y];
        Console.WriteLine("Chevy won by: " + cwin);
      }
      else if (ford[y] < chevy[x])
      {
        fn++;// ford wins
        fwin = ford[y] - chevy[x];
        Console.WriteLine("Ford won by: " + fwin);
      }
      else
      {
        Console.WriteLine("Tie!");// In tie no one wins so need not include this.
      }

    //output

    for (x = 0; x < 8; x++)
    {
        ctotal = ctotal + chevy[x];
    }
    chevyaverage = ctotal / 8;

    for (y = 0; y < 8; y++)
    {
        ftotal = ftotal + ford[y];
    }
    fordaverage = ftotal / 8;

    if(cn>fn)//checking the number of wins for each team
    {
        Console.WriteLine("Chevy wins");
    }
    else
        Console.writeLine("Ford wins");
    Console.WriteLine("The average time in seconds for each chevy car is: " + chevyaverage);
    Console.WriteLine("The average time in seconds for each ford car is: " + fordaverage);

    Console.ReadLine();

 }
}

好的,第一部分收集的数据将用于练习。

您需要调整的是找到赢家的逻辑。我将在您收集每个团队的时间后开始。

我建议做的是:

  1. 从 0..N-1 迭代(N = 8,这是数组长度)(我没有在你的代码中看到那个循环,希望我没有遗漏它)
  2. 迭代时做如下比较:

    int chevyWins = 0, fordWins = 0;
    for (int i = 0; i < 8; i++)
    {
        if (chevy[i] < ford[i])
        {
            //Note the difference with your code, your are doing 
            //the subtraction chevy[i] - ford[i] that will give you negative numbers.
            chevyWins++;
            Console.WriteLine(String.Format("Chevy won by {0}", (ford[i] - chevy[i])));
        }
        else if (chevy[i] > ford[i])
        {
            fordWins++;
            Console.WriteLine(String.Format("Ford won by {0}", (chevy[i] - ford[i])));
        }
        else
        {
            Console.WriteLine("Tie!");
        }
    }
    
    if (chevyWins > fordWins)
    {
        Console.WriteLine("Chevy Wins the competition!");
    }
    else if (chevyWins < fordWins)
    {
        Console.WriteLine("Ford Wins the competition!");
    }
    else
    {
        Console.WriteLine("The competition was tie!");
    }
    

    我认为您的代码的问题在于您没有迭代来比较每个场景。第二个是你如何计算每场比赛之间的差异。

Here 就是 fiddle.

希望这对你有用! 问候。

这是在 LinqPad 中完成的,因此您可以删除“.Dump()”,但您可以使用 .Zip 通过比较两个数组来完成您想要做的事情。为了示例目的,我已经缩短了代码,但您可以执行循环并将项目推入数组,然后将它们压缩以进行比较。

using System.Linq;

void Main()
{
    double[] chevy = { 1, 2, 1, 1 };
    double[] ford = { 2, 1, 1, 2 };

    var results = chevy.Zip(ford, (c, f) => 
    {
        if(c < f)
        {
            return "Chevy Won";
        }
        else if(f < c)
        {
            return "Ford Won";
        }
        else
        {
            return "It was a tie";
        }
    });

    results.Dump();
}

//output
    Chevy Won 
    Ford Won 
    It was a tie 
    Chevy Won