C# 将哨兵值排除在我的数组之外
本文关键字:我的 数组 排除 哨兵 | 更新日期: 2023-09-27 18:33:48
当我输入 10 个值时,此代码有效。如果我输入更少,我的哨兵值就会被添加。我希望它停止,并且能够操纵我的数组长度,这样当输入小于 10 时我就不会剩下多少 0。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace IntegerStatistics
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[10];
int arrayCount, high, low, sum;
double avg;
arrayCount = FillArray(numbers);
Statistics(numbers, arrayCount, out high, out low, out sum, out avg);
for (int x = 0; x < numbers.Length; ++x)
Write("{0, 4}", numbers[x]);
WriteLine();
WriteLine("The array has {0} values", arrayCount);
WriteLine("The highest value is {0}", high);
WriteLine("The lowest value is {0}", low);
WriteLine("The sum of the array is {0}", sum);
WriteLine("The average is {0}", avg);
}
private static int FillArray(int[] numbers)
{
const int QUIT = 999;
string enterNum;
int stop;
int count = 0;
int addNum = 0;
stop = numbers.Length - 1;
while((addNum != QUIT) && (count <= stop))
{
Write("Enter a number or 999 to exit: ");
enterNum = ReadLine();
while (!int.TryParse(enterNum, out numbers[count]))
{
WriteLine("Error");
Write("Enter a number or 999 to exit: ");
enterNum = ReadLine();
}
numbers[count] = Convert.ToInt32(enterNum);
addNum = numbers[count];
++count;
}
return count;
}
private static int Statistics(int[] numbers, int arrayCount, out int high, out int low, out int sum, out double avg)
{
high = numbers.Max();
low = numbers.Min();
sum = numbers.Sum();
avg = numbers.Average();
return arrayCount;
}
}
}
首先,让我们修复您的代码,因为它非常简单:与其在Main
中使用numbers.Length
,不如使用 arrayCount
。这是您已经拥有的东西,它将阻止Main
在最后显示零。
我想[...]来操纵我的数组长度
尽管 .NET 提供了一种调整数组大小的方法,但这不是您通常应该执行的操作,因为您的代码很快就会变得难以阅读。
解决此问题的更好方法是从 FillArray
返回大小合适的数组。但是,最好的解决方案是切换到使用 List<T>
,允许根据需要增长和收缩。
我已经修改了您的原始程序以使用List<int>
(良好做法)而不是动态调整大小的整数数组(一般来说,没有那么好的做法)。
class Program
{
static void Main(string[] args)
{
// Use a collection instead of an array, as length is as of yet unknown:
List<int> numbers;
int high, low, sum;
double avg;
numbers = FillArray();
Statistics(numbers, out high, out low, out sum, out avg);
foreach (var number in numbers)
{
Console.Write("{0, 4}", number);
}
Console.WriteLine();
Console.WriteLine("The array has {0} values", numbers.Count);
Console.WriteLine("The highest value is {0}", high);
Console.WriteLine("The lowest value is {0}", low);
Console.WriteLine("The sum of the array is {0}", sum);
Console.WriteLine("The average is {0}", avg);
Console.ReadKey();
}
private static List<int> FillArray(int maximum = 10)
{
const int QUIT = 999;
int count = 0;
int addNum = 0;
var list = new List<int>();
while (count <= maximum)
{
Console.Write("Enter a number or 999 to exit: ");
if (!int.TryParse(Console.ReadLine(), out addNum))
{
Console.WriteLine("Error");
continue;
}
if (addNum == QUIT)
{
break;
}
list.Add(addNum);
count++;
}
return list;
}
private static void Statistics(List<int> numbers, out int high, out int low, out int sum, out double avg)
{
high = numbers.Max();
low = numbers.Min();
sum = numbers.Sum();
avg = numbers.Average();
}
}
我还注意到您在收集numbers
时包括了999
的"转义"值。 我更正了这一点,以便999
不包括在计算的平均值中(我猜这是您的意图)。
类似于@Dave。允许值为 999。没有异常处理,...
class Program
{
private static string _STOP = "STOP";
private static int _MAX_SIZE = 10;
static void Main(string[] args)
{
List<int>numbers = FillList();
foreach(int number in numbers)
Console.Write("{0, 4}", number);
Console.WriteLine();
Console.WriteLine("The list has {0} values", numbers.Count);
Console.WriteLine("The highest value is {0}", numbers.Max());
Console.WriteLine("The lowest value is {0}", numbers.Min());
Console.WriteLine("The sum of the array is {0}", numbers.Sum());
Console.WriteLine("The average is {0}", numbers.Average());
Console.ReadKey();
}
private static List<int> FillList()
{
List<int> numbers = new List<int>();
int value;
int count = 0;
do
{
Console.Write("Enter a number or {0} to exit: ", _STOP);
string line = Console.ReadLine();
if (line == _STOP)
break;
if (int.TryParse(line, out value))
{
numbers.Add(value);
count++;
}
else
{
Console.WriteLine("Error reading number.");
}
} while (count < _MAX_SIZE);
return numbers;
}
}