【问题标题】:How to make a Partially Filled Array in C#如何在 C# 中创建一个部分填充的数组
【发布时间】:2014-11-22 02:47:19
【问题描述】:

我正在尝试在控制台应用程序中使用此代码创建一个 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);


        }
    }
}

【问题讨论】:

  • 您的实际问题是什么? 具体你有什么困难?见stackoverflow.com/help/how-to-ask
  • 我希望能够使用数组输入多个值,但如果我没有在 {int [] score = {0, 1, 2, ...];} 中为每个参数输入值它应该能够将用户输入的数字相加,而忘记其余的。例如,如果我在一行输入 56 76 86,然后输入 0 终止数组,它将相加 56 76 86,并且不需要其他数字来填充数组。
  • @GoodyGoodmansen 那么你应该使用List&lt;int&gt; 而不是数组。数组必须有固定的大小;列表没有。
  • @D Stanley 我想拆分它,以便将所有值输入一行,然后加在一起,但可以部分输入。

标签: c# arrays algorithm


【解决方案1】:

看看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];

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

【讨论】:

  • 我从 int[] score = line.Split(); 得到一个错误说明它不能将字符串隐式转换为 int。
【解决方案2】:

我仍然不确定我是否理解您遇到的问题。但我认为对您的代码进行以下更改应该可以实现您想要的:

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);

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

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

【讨论】:

  • 我想拆分这个,这样所有的值都输入一行,然后加在一起,但可以部分输入。
【解决方案3】:

我想我明白了:

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 填充数组的其余部分,现在您只能添加您输入的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多