![](/img/trans.png)
[英]Linq Groupby categorizes but how do I manipulate the values in the collection
[英]How do I create an infinite loop with a question that when answered, categorizes the answer into 1 of 3 different arrays?
我有 3 个不同的类别。 1.幼儿(1-4岁) 2.儿童(5-17岁) 3.成人(18岁及以上)
我想无限循环“给一个数字”,这样即使所有 3 个数组都已填满,它仍然会继续。 我希望循环仅在键入 0 时结束。 (这我似乎管理)只需要存储前十个值,数组填充后的任何值都可以忽略。 (每 3 个数组 10 个值,总共 30 个值)
然后我想按照上面的解释对年龄进行分类。 对于 3 个数组中的每个元素,我当前的年龄输出(“输入”)都保持为 0。 我意识到我的 if 语句不会因为“...长度”而被触发,但是您建议如何替换它? 我尝试了“i < 10”,但这会导致数组越界异常,以及将 todlers[i]/children[i]/grownups[i] < 10
对不起,如果我不够简洁,但我仍然是初学者,英语不是我的母语。 提前致谢。
class Program
{
static void Main(string[] args)
{
int[] todlers = new int[10];
int[] children = new int[10];
int[] grownups = new int[10];
for (int i = 0; ; i++)
{
Console.Write("Give a number (0 = stop) : ");
int input = int.Parse(Console.ReadLine());
if (todlers.Length < 10 && input < 5 && input > 0)
{
todlers[i] = input;
}
else if (children.Length < 10 && input > 4 && input < 18)
{
children[i] = input;
}
else if (grownups.Length < 10 && input > 17)
{
grownups[i] = input;
}
else if (input == 0)
{
break;
}
else
{
continue;
}
}
Console.Write("\n");
Console.WriteLine("TODLERS");
for (int i = 1; i < 11; i++)
{
Console.WriteLine("Todler {0} is {1} years old", i, todlers[i]);
}
Console.Write("\n");
Console.WriteLine("CHILDREN");
for (int i = 1; i < 11; i++)
{
Console.WriteLine("Child {0} is {1} years old", i, children[i]);
}
Console.Write("\n");
Console.WriteLine("GROWNUPS");
for (int i = 1; i < 11; i++)
{
Console.WriteLine("Man/Woman {0} is {1} years old", i, grownups[i]);
}
Console.ReadKey();
}
}
数组的Length
不会改变,它在整个程序中与初始化 (10) 时保持不变。 结果todlers.Length < 10
总是错误的。
鉴于您从未将0
放入数组中,您可以将todlers.Length < 10
替换为todlers[9] == 0
,这将有效地检查您是否尚未更改数组中的最后一个元素。
for (int i = 1; i < 11; i++)
你应该改成
for (int i = 0; i < 10; i++)
这是因为您已经用 10 个元素初始化数组,第一个元素将为 0
幼儿[0]、幼儿[1]...幼儿[9]
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/
当您不知道将拥有多少项时,使用List
来存储事物的集合。 使用do - while
循环至少运行一次代码块,最多无限次运行。
尝试这样的事情(未经测试的代码):
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
List<int> todlers = new List<int>();
List<int> children = new List<int>();
List<int> grownups = new List<int>();
int input = 0;
do
{
Console.Write("Give a number (0 = stop) : ");
input = int.Parse(Console.ReadLine());
if (todlers.Count < 10 && input < 5 && input > 0)
{
todlers.Add(input);
}
else if (children.Count < 10 && input > 4 && input < 18)
{
children.Add(input);
}
else if (grownups.Count < 10 && input > 17)
{
grownups.Add(input);
}
}while(input>0);
Console.Write("\n");
Console.WriteLine("TODLERS");
for (int i = 0; i < todlers.Count; i++)
{
Console.WriteLine("Todler {0} is {1} years old", i+1, todlers[i]);
}
Console.Write("\n");
Console.WriteLine("CHILDREN");
for (int i = 0; i < children.Count; i++)
{
Console.WriteLine("Child {0} is {1} years old", i+1, children[i]);
}
Console.Write("\n");
Console.WriteLine("GROWNUPS");
for (int i = 0; i < grownups.Count; i++)
{
Console.WriteLine("Man/Woman {0} is {1} years old", i+1, grownups[i]);
}
Console.ReadKey();
}
}
当有 10 个条目时,这将停止添加到列表中,但用户不会收到他们的输入被丢弃的警告。 如果用户在一个类别中输入少于十个,它也将只输出已添加的条目数,而不是零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.