根据用户输入寻找数组中的最大值(C#)
这里的学生。根据用户输入寻找数组中的最大值(C#)
目前正在一个项目中寻找基于用户输入的数组中的最高值。
我正在使用的当前foreach循环获取用户输入,然后只找到匹配第二个数组的第一个实例,而不是继续循环。
我试过两种方法。两者都以相同的结果结束。
首先,我尝试创建一个列表,然后排序和反转。这样我可以拿0指数,它是最高
static void Main(string[] args)
{
string[] fishColors = new string[15] { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", "red", "orange", "purple", "green", "red", "purple" };
int[] fishLengths = new int[15] { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 };
List<int> userFishLengths = new List<int>();
int userChoice = 0;
string input = null;
int longestFish = 0;
do {
Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:\r\n0. Pink\r\n1. Purple\r\n2. Red\r\n3. Orange\r\n4. Blue\r\n5. Green");
input = Console.ReadLine();
} while (Int32.TryParse(input, out userChoice) == false) ;
string userColor = fishColors[userChoice];
foreach (string fish in fishColors)
{
if (userColor == fish)
{
int indexID = Array.IndexOf(fishColors, fish);
int fishLength = fishLengths[indexID];
userFishLengths.Add(fishLength);
}
}
userFishLengths.Sort();
userFishLengths.Reverse();
Console.WriteLine("The longest fish in the tank with the color you chose (" + userColor + ") is " + userFishLengths[0]+" inches.");
}
其次,我试图创建一个值,它需要每次都在,并覆盖变量,如果是较大的。
static void Main(string[] args)
{
string[] fishColors = new string[15] { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", "red", "orange", "purple", "green", "red", "purple" };
int[] fishLengths = new int[15] { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 };
int userChoice = 0;
string input = null;
int longestFish = 0;
do {
Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:\r\n0. Pink\r\n1. Purple\r\n2. Red\r\n3. Orange\r\n4. Blue\r\n5. Green");
input = Console.ReadLine();
} while (Int32.TryParse(input, out userChoice) == false) ;
string userColor = fishColors[userChoice];
foreach (string fish in fishColors)
{
if (userColor == fish)
{
int indexID = Array.IndexOf(fishColors, fish);
int fishLength = fishLengths[indexID];
if (fishLength > longestFish)
{
longestFish = fishLength;
}
}
}
Console.WriteLine("The longest fish in the tank with the color you chose (" + userColor + ") is " + longestFish + " inches.");
}
任何帮助/建议,将不胜感激。谢谢!
问题在于你的Array.IndexOf()
调用。
int indexID = Array.IndexOf(fishColors, fish);
您fishColors
数组的内容不是唯一的,并且因此Array.IndexOf(fishColors, fish)
呼叫被简单地返回第一匹配元素的索引。 ("pink" = 0
例如,"red" = 2
)
你会使用不同的数据结构来存储这些值更适合。使用Dictionary<TKey,TValue>
进行研究,例如
var fish = new Dictionary<string, int[]>()
{
{ "pink", new[] { 49, 44 } },
{ "purple", new[] { 5, 17, 37 } }
};
这会让你更容易查找与颜色相关的长度。
或者,如果你一定要保留使用两个数组,你可以用一个简单的for
循环,而不是一个foreach
做到这一点。
for (int i = 0; i < fishColors.Length; i++)
{
if (userColor == fishColors[i])
{
int fishLength = fishLengths[i];
if (fishLength > longestFish)
{
longestFish = fishLength;
}
}
}
谢谢你的快速和明确的回应。 我同意,这可能是最简单的方法,并且最有意义。然而为了学校的项目,我必须使用两个数组。 (抱歉,我输入太快) –
@DylanColeDuke好的,让我写出你的第二个答案与这些约束。 (编辑 - 检查添加内容。) –
是的,可以满足这些要求。这是完美的,谢谢你的帮助! –
脚本没有工作,因为你使用:
int indexID = Array.IndexOf(fishColors, fish);
总是给你的第一场比赛,而不是目前的对。
例如: 您正在搜索“purple”,并始终输入条目5。
如果我不想改变你的代码了,那么这将改变版本:
static void Main(string[] args)
{
string[] fishColors = new string[15] { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", "red", "orange", "purple", "green", "red", "purple" };
int[] fishLengths = new int[15] { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 };
int userChoice = 0;
string input = null;
int longestFish = 0;
do
{
Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:\r\n0. Pink\r\n1. Purple\r\n2. Red\r\n3. Orange\r\n4. Blue\r\n5. Green");
input = Console.ReadLine();
} while (Int32.TryParse(input, out userChoice) == false);
string userColor = fishColors[userChoice];
int indexID = 0;
foreach (string fish in fishColors)
{
if (userColor == fish)
{
int fishLength = fishLengths[indexID];
if (fishLength > longestFish)
{
longestFish = fishLength;
}
}
indexID++;
}
Console.WriteLine("The longest fish in the tank with the color you chose (" + userColor + ") is " + longestFish + " inches.");
Console.ReadKey();
}
但随着LINQ,你可以使它更简单:
static void Main(string[] args)
{
string[] fishColors = new string[15] { "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue", "red", "orange", "purple", "green", "red", "purple" };
int[] fishLengths = new int[15] { 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 };
int userChoice = 0;
string input = null;
int longestFish = 0;
do
{
Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:\r\n0. Pink\r\n1. Purple\r\n2. Red\r\n3. Orange\r\n4. Blue\r\n5. Green");
input = Console.ReadLine();
} while (Int32.TryParse(input, out userChoice) == false);
string userColor = fishColors[userChoice];
longestFish = fishColors
.Zip(fishLengths, (color, length) => new { color, length })
.Where(s => s.color.Equals(userColor)).Max(x => x.length);
Console.WriteLine("The longest fish in the tank with the color you chose (" + userColor + ") is " + longestFish + " inches.");
Console.ReadKey();
}
这是完美的!谢谢。 –
我知道,这是学生项目;但问题是非常一个的LINQ已被设计为
string[] fishColors = new string[] //DONE: you have no need in specifing magic number "15"
{ "pink", "purple", "red", "orange", "blue", "green", "pink", "green", "blue",
"red", "orange", "purple", "green", "red", "purple" };
int[] fishLengths = new int[] //DONE: you have no need in specifing magic number "15"
{ 49, 5, 45, 10, 14, 1, 44, 17, 48, 11, 13, 17, 20, 15, 37 };
// Color/its index correspondence:
// Key - index: 1, 2, 3, ...
// Value - color: pink, purple, red, ...
var colors = fishColors
.Distinct()
.Select((color, index) => new {
color = color,
index = index + 1, })
.ToDictionary(item => item.index, item => item.color);
string userColor = null;
while (true) {
Console.WriteLine("Please select the number from the list below for the color of fish you would like to choose:");
//DONE: instead of hardcoding, build the string
Console.WriteLine(string.Join(Environment.NewLine, colors
.OrderBy(pair => pair.Key)
.Select(pair => $"{pair.Key}. {pair.Value}")));
//DONE: input is valid if and only iff it's integer and it corresponds to color
if (int.TryParse(Console.ReadLine(), out var code) && // <- out var - C# 7.0 Syntax
colors.TryGetValue(code, out userColor))
break;
}
//DONE: zip colors and lengths, filter out userColor fish only, get maximum
var result = fishColors
.Zip(fishLengths, (color, length) => new { color = color, length = length })
.Where(item => item.color == userColor)
.Max(item => item.length);
//DONE: do not concat string, but use string interpolation (or formatting)
Console.WriteLine($"The longest fish in the tank with the color you chose ({userColor}) is {result} inches.");
问题是什么时,用户应该选择一种颜色,程序,长度响应? –
正确,程序以用户所选颜色的最大长度进行响应。 –
好的,我会和Daniel Mays一起回答。如果你需要颜色作为一个唯一的标识符,那么将它们作为关键字的字典和它们对应的长度数组作为值将是最好的方法。 –