为什么不将用户输入的最小数字添加到列表中?
Why doen't the smallest number from user input gets added to the list?
学校有一个测试,我们必须编写一个 C# 控制台程序,从用户输入中读取小于 100 的正整数,然后写出一些细节,例如最大的数字。我用一个列表来存储数字。我用我输入的一些随机数对其进行了测试,但程序只将数字添加到列表中,从第二小的数字开始。
int count = 0;
List<int> bekertek = new List<int>();
int bekert = Convert.ToInt32(Console.ReadLine());
double atlag;
while (bekert > 0 && bekert < 100)
{
bekert = Convert.ToInt32(Console.ReadLine());
count++;
bekertek.Add(bekert);
}
/* This section is only for checking the list's elements
foreach (var i in bekertek)
{
Console.Write(i + " ");
}*/
Console.WriteLine("A bevitt adatok száma: {0}", count);
bekertek.Sort();
bekertek.Remove(bekertek.Last());
atlag = bekertek.Average();
Console.WriteLine("A legnagyobb érték: {0}", bekertek.Last());
Console.WriteLine("A legkisebb érték: {0}", bekertek.First());
Console.WriteLine("Az adatok átlaga: {0}", atlag);
我做错了什么?
对于控制台中的输入,我认为 do...while 循环是理想的结构。菜单或请求号码 - 它涵盖了所有这些。
至于出了什么问题:实际上它把所有的数字都加到了集合中(调试代码应该可以确认)。您只需从排序和显示之间的集合中删除最小的数字:
bekertek.Sort();
bekertek.Remove(bekertek.Last()); //Should not have done this.
atlag = bekertek.Average();
有句话说:"The 2 most common issues in Progamming is naming variables, chache invalidation and off-by-one errors."但是这个具体的制作有点独特:)
编辑:正如 Biesi Grr 指出的,您还忽略了第一个输入:
int bekert = Convert.ToInt32(Console.ReadLine()); //this is never processed
double atlag;
while (bekert > 0 && bekert < 100)
{
bekert = Convert.ToInt32(Console.ReadLine());
count++;
bekertek.Add(bekert);
}
也许您想在这里使用 ReadKey()?也不需要计数器 - 这实际上是 List.Lenght 的工作。在任何情况下,do..while 都会使该行变得不必要。
让我们做一个固定的,做...而版本
bool repeat= true;
do{
int bekert = Convert.ToInt32(Console.ReadLine());
if(bekert > 0 && bekert < 100)
bekertek.Add(bekert);
else
repeat = false;
}while(repeat )
学校有一个测试,我们必须编写一个 C# 控制台程序,从用户输入中读取小于 100 的正整数,然后写出一些细节,例如最大的数字。我用一个列表来存储数字。我用我输入的一些随机数对其进行了测试,但程序只将数字添加到列表中,从第二小的数字开始。
int count = 0;
List<int> bekertek = new List<int>();
int bekert = Convert.ToInt32(Console.ReadLine());
double atlag;
while (bekert > 0 && bekert < 100)
{
bekert = Convert.ToInt32(Console.ReadLine());
count++;
bekertek.Add(bekert);
}
/* This section is only for checking the list's elements
foreach (var i in bekertek)
{
Console.Write(i + " ");
}*/
Console.WriteLine("A bevitt adatok száma: {0}", count);
bekertek.Sort();
bekertek.Remove(bekertek.Last());
atlag = bekertek.Average();
Console.WriteLine("A legnagyobb érték: {0}", bekertek.Last());
Console.WriteLine("A legkisebb érték: {0}", bekertek.First());
Console.WriteLine("Az adatok átlaga: {0}", atlag);
我做错了什么?
对于控制台中的输入,我认为 do...while 循环是理想的结构。菜单或请求号码 - 它涵盖了所有这些。
至于出了什么问题:实际上它把所有的数字都加到了集合中(调试代码应该可以确认)。您只需从排序和显示之间的集合中删除最小的数字:
bekertek.Sort();
bekertek.Remove(bekertek.Last()); //Should not have done this.
atlag = bekertek.Average();
有句话说:"The 2 most common issues in Progamming is naming variables, chache invalidation and off-by-one errors."但是这个具体的制作有点独特:)
编辑:正如 Biesi Grr 指出的,您还忽略了第一个输入:
int bekert = Convert.ToInt32(Console.ReadLine()); //this is never processed
double atlag;
while (bekert > 0 && bekert < 100)
{
bekert = Convert.ToInt32(Console.ReadLine());
count++;
bekertek.Add(bekert);
}
也许您想在这里使用 ReadKey()?也不需要计数器 - 这实际上是 List.Lenght 的工作。在任何情况下,do..while 都会使该行变得不必要。
让我们做一个固定的,做...而版本
bool repeat= true;
do{
int bekert = Convert.ToInt32(Console.ReadLine());
if(bekert > 0 && bekert < 100)
bekertek.Add(bekert);
else
repeat = false;
}while(repeat )