如何将数组拆分为两个不同的数组,以及如何允许用户仅输入数组的一部分而不是最大数量

本文关键字:数组 用户 何允许 输入 最大数 一部分 拆分 两个 | 更新日期: 2023-09-27 18:32:07

我不知道如何正确拆分我的数组,以便我可以将这两个单独的数组传递给多个不同的方法。我还想在任何时候结束数组,而不会让程序给用户错误。任何人都可以给我一些关于该怎么做并帮助我修复程序的指导吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace proj09LEA
{
    class Program
    {
        // declare a constant integer
        const int MAX = 10;
        static void Main(string[] args)
        {
            // declare and array of integers
            int[] array = new int[MAX];
            Console.WriteLine("'nSaturday Coder's Bowling Team");
            Console.WriteLine("Enter in a name and score for each person on the team.");
            Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.'n");
            // fill an array with user input
            for (int i = 0; i < array.Length; i++)
            {
                // ask the user to enter some data
                Console.WriteLine("Enter in a name and score: ");
                // the line of data input by the user is stored here
                string userInput = Console.ReadLine();
                // userInput is split into two pieces, which are stored in this array
                string[] parsedInput;
                // this line splits the string userInput into two pieces
                parsedInput = userInput.Split();
                // store the first piece, the name, in a string array
                string names = parsedInput[0];
                // store the second piece, a score, in an integer variable
                int scores = int.Parse(parsedInput[1]);
                array[i] = scores;
            }
            Console.WriteLine("------------ Input Complete ------------'n");
            Console.WriteLine("Here are the scores for this game:");
            // display the scores for each person from method DisplayScores
            DisplayScore(array);
            // display the highest score and name of player from method HighScore
            HighScore(array);
            // display the lowest score and name of player from method LowScore
            LowScore(array);
            // display the average score from method AverageScore
            AverageScore(array);
            Console.WriteLine("Press Enter to continue. . .");
            Console.ReadLine();
        }
        static void DisplayScore(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine("{0}'s score was {0}.'n", array[i]);
            }
        }
        static void HighScore(int[] array)
        {
            int max = array.Max();
            Console.WriteLine("Congratulations {0}, your score of {0} was the highest.", max);
        }
        static void LowScore(int[] array)
        {
            int min = array.Min();
            Console.WriteLine("{0}, your score of {0} was the lowest. Better get some practice.", min);
        }
        static void AverageScore(int[] array)
        {
            int sum = array.Sum();
            int average = sum / array.Length;
            Console.WriteLine("The average score for this game was {0:d}.", average);
        }
    }
}

如何将数组拆分为两个不同的数组,以及如何允许用户仅输入数组的一部分而不是最大数量

如果您愿意逐个输入名字,则在逐个得分之后

Console.WriteLine("Enter in a name: ");
string userInput = Console.ReadLine();
for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine("Enter in a score "+i+": ");
                string score = Console.ReadLine();
                // store the second piece, a score, in an integer variable
                int scores = int.Parse(score);
                array[i] = scores;
            }