我如何比较一个数组中的数字与另一个数组中的数字并找到差异?我正在使用c#
本文关键字:数组 数字 一个 何比较 另一个 比较 | 更新日期: 2023-09-27 18:10:15
我遇到了以下问题,我不知道如何比较两个数组并显示两者之间的差异或显示它是一个平局。请帮忙做这个练习。我目前拥有的代码不允许我找到每辆车的差异。我不知道我该做什么。
每个队有八辆车,分别叫雪佛兰和福特。每队各有一辆车在阻力带上与对手比赛。读一读八辆雪佛兰汽车的比赛时间,然后读一读八辆福特汽车的比赛时间。将时间存储到名为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[]。
•接受每辆福特赛车的比赛时间到数组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();
}
}
好的,第一部分收集了用于练习的数据。
你需要知道的是找到赢家的逻辑。我会在你收集每个队的成绩后开始。
我的建议是:
- 从0..N-1 (N = 8,这是数组长度)(我没有看到循环在你的代码,我希望我没有错过它)
-
在迭代时做如下比较:
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!"); }
我认为你的代码的问题是你没有迭代比较每个场景。第二是你如何计算每场比赛的差异。
小提琴来了。
我希望这对你有用!问候。
试试这个,你已经为问题的主要部分写好了,你添加变量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();
}
}
这是在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