您好,我们目前正在执行数组任务,但我是C#

时间:2016-09-20 23:22:14

标签: c#

我们必须找到用户输入的5个数字的平均值或总和,但我不确定如何做到这一点。任何帮助表示赞赏!到目前为止这是我的代码: 基本上我需要帮助来完成输入数字的总和,以及如何使用稍后在代码中输入的数字来使用数组,因为它宣称'int unassigned'。

{
    class Program
    {
        static void Main(string[] args)
        {
            int numofnum;
            int[] numbers = new int[numofnum];
            int index;
            string avsu;
            int average;
            index = 0;

            Console.WriteLine("Please enter the number of numbers you would like to find the average for, or add together: ");
            numofnum = int.Parse(Console.ReadLine());


            Console.WriteLine("Would you like to find the average or sum of these numbers? (average/sum)");
            avsu = Console.ReadLine();


            if (avsu == "average")
            {


            do 
            {
                Console.Write("Please enter Number to find the average of: " + index);
                numbers[index] = int.Parse(Console.ReadLine());
                index = index + 1;
            } while (index < numofnum);

            index = 0;

            do
            {
                Console.WriteLine("You entered the following values: ");
                Console.WriteLine("Number " + index + ": " + numbers[index]);
                index = index + 1;

                Console.WriteLine("The average of these numbers is: " + (numbers[index] / 2));
            } while (index < numofnum);

                else if (avsu == "sum")
            {


            do 
            {
                Console.Write("Please enter Number " + index + " to add: ");
                numbers[index] = int.Parse(Console.ReadLine());
                index = index + 1;
            } while (index < numofnum);

            index = 0;

            do
            {
                Console.WriteLine("You entered the following values: ");
                Console.WriteLine("Number " + index + ": " + numbers[index]);
                index = index + 1;

                Console.WriteLine("The sim of these numbers is: " + (numbers[index])); // UNSURE HOW TO DISPLAY THE SUM!!!!
            } while (index < numofnum);
            Console.ReadLine();

            }


        }
    }
}

1 个答案:

答案 0 :(得分:0)

这会有帮助吗?

-q