搜索数组并找到最大的对象

时间:2017-07-29 04:27:13

标签: c#

我是C#的初学者,我有一项任务是创建一个程序来对两个数组进行排序,并根据用户颜色输入找到最大的鱼。我不知道如何使它工作。任何人都可以给我一些指示吗?这是我到目前为止所拥有的。

Console.WriteLine("Select a fish color. \nEnter only the number.\n1. 
Black\n2. Green\n3. Red\n4. Blue\n5. Yellow\nEnter Your Selection:");

string userColor = Console.ReadLine();
int value;
while (!(int.TryParse(userColor, out value) && value >=1 && value <=5))
{
    Console.WriteLine("Please type only numbers from 1 to 5");
    userColor = Console.ReadLine();
}

int color = int.Parse(userColor);

string[] colors = new string[] { "Black", "Black", "Green", "Red", "Blue", "Yellow", "Black", "Green", "Red", "Blue", "Yellow", "Black", "Green", "Red", "Blue","Yellow"};

double[] lenght = new double[] { 8.5, 9.8, 10.9, 40, 36, 45, 43, 12, 17.8, 13.5, 14.9, 27.7, 26.9, 50, 60,13 };

Console.WriteLine("You chose " + colors[color]);
Console.WriteLine("The biggest " + colors[color] + " fish is " + lenght[color]+ " inches long.");

1 个答案:

答案 0 :(得分:0)

所以,你需要做两件事:

  1. 找到与您要搜索的颜色相匹配的每条鱼。
  2. 找到该颜色鱼中最大的相应数字。
  3. 我会尝试给你一个开始的地方,以及你可能使用的工具的一个例子:

    • 您应该使用for循环,因为这样可以让您轻松遍历两个列表中的值。
    • 将用户输入数字映射为字符串颜色后,可以使用==比较鱼的颜色。
    • 您可以在自己的变量中跟踪到目前为止发现的最大鱼群长度,并在需要时进行更新。

    (PS我故意保持模糊和新手友好。如果我为自己解决这个问题,我可能会尝试找到一种方法来解决2-3行中的这个问题,但这需要比你更多的理解到目前为止。)