如何在C#中制作部分填充数组

时间:2014-11-22 02:47:19

标签: c# arrays algorithm

我正在尝试在控制台应用程序中使用此代码创建一个Partial数组。我要做的是在一个Console.ReadLine上输入多个值(示例测试分数),取所输入数字的总和,但如果用户输入小于LIMIT,例如输入5个值,但是有空间用于总共10个,它只会将这5个值加起来。

我希望能够在一行中使用数组输入多个值,但是如果我不在int [] scores = {0, 1, 2, ...];中输入每个参数的值,它应该能够累加用户输入的数字,并且别忘了。例如,如果我在一行输入56 76 86,则输入0终止数组,它将加起来56 76 86,并且不需要其他数字来填充数组。

    class Program
    {
        const int LIMIT = 10;

        static void Main(string[] args)
        {
            //Declarations:
            //Array Size
            //Array Scope




            int[] examScores = new int[LIMIT];

            //Define an Array of integers:
            int testNum = 1;
            int [] scores = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            //1. Ask User Method:
                //a.)Ask user to input numbers.
                //b.)Save user numbers in an array

            Console.WriteLine("Input all of your test scores as the program prompts of");
            Console.WriteLine("each score on the lines below. (i.e. 89 25 87 98...)");


           //Purpose of this for method is to get user input and save to
           //an array.

            for (int i = 0; i < scores.Length; i++)
            {
                Console.WriteLine("\nEnter test score #{0}", testNum);
                scores[i] = int.Parse(Console.ReadLine());
            }
            PrintScores(scores);




            Console.Read();
        }//End Main.

        //2. AddSum Method. 
        //Purpose: Take users input and add all numbers together.
        //Paramiters: Array numbers from Main saved as PrintScores
        //Returns: None
        //Prints: Sum of Scores.
        static void PrintScores(int[] scr)
        {
            int result = 0;

            for (int i = 0; i < scr.Length; i++)
            {
                result += scr[i];
            }

            Console.WriteLine("\n--------------------------------------");
            Console.WriteLine("Sum of your test scores equal: {0}", result);


        }
    }
}

3 个答案:

答案 0 :(得分:0)

查看string.Split()方法,该方法根据分隔符将字符串拆分为多个部分。

在这种情况下,只需读取单个Console.ReadLine()中的所有文本,然后将字符串拆分为数组,解析它们,并将它们插入examScores数组中。

string line = Console.ReadLine(); //Read the line of scores (Ex: 89 25 87 98)
int[] scores = line.Split(' '); //Split it between the spaces, into an array

for (int i = 0; i < input.Length; i++) //For each element in this array, set the score array to the same
    examScores[i] = scores[i];

它只会复制输入的分数,其余分数将保持不变。

答案 1 :(得分:0)

我仍然不确定我理解你遇到麻烦的部分。但我认为您的代码的以下更改应该可以实现您的目标:

List<int> scores = new List<int>();

Console.WriteLine("Input all of your test scores as the program prompts of");
Console.WriteLine("each score on the lines below. (i.e. 89 25 87 98...)");

string line;

Console.WriteLine("\nEnter test score #1");
while ((line = Console.ReadLine()) != "")
{
    scores.Add(int.Parse(line));
    Console.WriteLine("\nEnter test score #{0}", scores.Count + 1);
}
PrintScores(scores.ToArray());

以上将允许用户每行输入一个分数(根据您的原始代码示例),当他们只需按 Enter 而不输入任何内容(即他们输入空白行)时,它将停止尝试阅读更多分数,并会调用PrintScores()方法添加分数。

编辑:根据您的说明,原始代码示例不能代表您想要的行为,以下是另一种实现:

Console.WriteLine("Input all of your test scores as the program prompts of");
Console.WriteLine("each score on the lines below. (i.e. 89 25 87 98...)");

Console.WriteLine("\nEnter test scores: ");
int[] scores = Console.ReadLine().Split(' ').Select(text => int.Parse(text)).ToArray();
PrintScores(scores);

这将从用户读取单个行,其中在输入的每个整数之间有一个空格。它会将单个整数从其文本格式转换为实际int,最后将结果复制到实际的int[]中,以便将其传递给PrintScores()方法。

如果要允许用户在每个数字之间输入任意数量的空格,请参阅String.Split()方法的文档,以获取要从拆分结果中删除空元素的选项。

答案 2 :(得分:0)

我想我明白了:

for (int i = 0; i < scores.Length; i++)
{
    Console.Write("\nEnter test score #" +(i+1) +": ");
    scores[i] = int.Parse(Console.ReadLine());
    if (scores[i] == 0 )
    {
        while (i < scores.Length)
        {
            scores[i]=0;
            i++;
        }

    }

}
PrintScores(scores);

这样,当你输入0时,它将用0&#39; s填充数组的其余部分,现在你只能添加你输入的值。