无法从List <int>输出元素

时间:2016-11-27 20:15:28

标签: c# list arraylist

我需要向ArrayList添加一些整数,然后第一平均值不会高于或低于第二平均值。但最后,我需要输出所有元素,我不能这样做。我尝试使用List<>代替ArrayList,但我遇到了输出问题。

这是我的代码:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;

 namespace Marks
 {
    internal class Program
    {
        public static float AverageOfArrayList(ArrayList Array)
        {
            float AverageOfArrayList = 0;
            float Sum = 0;

            foreach (int item in Array)
            {
               Sum += item;
            }
            AverageOfArrayList = Sum / Array.Count;
            return AverageOfArrayList;
        }
        public static ArrayList GetVariables(ArrayList NewMarksList, float CurrentAverage, double MissedAverage)
        {
            if (CurrentAverage > MissedAverage)
            {
               for (int i = 0; MissedAverage < AverageOfArrayList(NewMarksList) ; i++)
               {
                  NewMarksList.Add(2);
               }
            }

            if (CurrentAverage < MissedAverage)
            {
                  for (int i = 0; AverageOfArrayList(NewMarksList) < MissedAverage; i++)
                  {
                     NewMarksList.Add(5);
                  }
            }
            return NewMarksList;
      }
      static void Main(string[] args)
      {
          float CurrentAverage = 0;
          double MissedAverage = 0;
          ArrayList CurrentMarksList = new ArrayList();


          Console.WriteLine("Enter how much marks have you got");
          int CountOfMarks = Convert.ToInt16(Console.ReadLine());

          Console.WriteLine("Enter the 1-st average");
          CurrentAverage = float.Parse(Console.ReadLine());

          Console.WriteLine("Enter the 2-nd average");
          MissedAverage = Convert.ToDouble(Console.ReadLine());

          ArrayList newList = GetVariables(CurrentMarksList, CurrentAverage, MissedAverage);
          List<int> OutputArray = newList.Cast<int>().ToList();

          Console.WriteLine("Marks to add :");

          for (int OutputCounter = 0; OutputCounter < OutputArray.Count; OutputCounter++)
          {
              Console.Write(OutputArray[OutputCounter] + " ");
          }
          Console.ReadKey();
      }
   }
}

我用来测试程序的输入:

 CountOfMarks = 5;
 CurrentAverage = 3,4;
 MissedAverage = 3,8;

 Output = Nothing

1 个答案:

答案 0 :(得分:0)

如果你能给出一个输入的例子,你期望的和现实中发生的事情,那就是eaiser。

这是我初始化参数的方式:

;

顺便说一下,不需要将空列表传递给函数,然后返回一个新列表 您可以将函数内的空列表声明为局部变量。

此代码存在两个大问题:

将数字除以

CountOfMarks = 3 CurrentAverage = 4 MissedAverage = 5 内,您使用了GetVariables(...) 问题是你将一个空数组传递给这个函数 空数组的计数为零,您将数字除以AverageOfArrayList(NewMarksList)非法

因此,您无法向Sum / Array.Count发送空数组。

我通过在AverageOfArrayList(...)循环之前添加o_NewMarksList.Add(...)来解决它,并将索引增加到for作为替代。
这将使您发送一个包含列表一元素的数组 我还将空列表声明为局部变量。

For循环中的不可更改条件导致无限循环


你把i = 1循环的条件放在一个平均值上,它总是相同的 在第一个循环中,平均值将始终为2,而在第二个循环中,它总是为5 这将导致循环无限运行 为了解决这个问题,我们需要了解你真正想做的事情。

替代解决方案(仅解决除零)

for

编辑(2016年11月30日):
第二次尝试:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Marks
{
    internal class Program
    {
        public static float AverageOfArrayList(ArrayList Array)
        {
            float AverageOfArrayList = 0;
            float Sum = 0;

            foreach (int item in Array)
            {
                Sum += item;
            }
            AverageOfArrayList = Sum / Array.Count;
            return AverageOfArrayList;
        }
        public static ArrayList GetVariables(float CurrentAverage, double MissedAverage)
        {
            ArrayList o_NewMarksList = new ArrayList();
            if (CurrentAverage > MissedAverage)
            {
                o_NewMarksList.Add(2);
                for (int i = 1; MissedAverage < AverageOfArrayList(o_NewMarksList); i++)
                {
                    o_NewMarksList.Add(2);
                }
            }

            if (CurrentAverage < MissedAverage)
            {
                o_NewMarksList.Add(5);
                for (int i = 1; AverageOfArrayList(o_NewMarksList) < MissedAverage; i++)
                {
                    o_NewMarksList.Add(5);
                }
            }
            return o_NewMarksList;
        }
        static void Main(string[] args)
        {
            float CurrentAverage = 0;
            double MissedAverage = 0;



            Console.WriteLine("Enter how much marks have you got");
            int CountOfMarks = Convert.ToInt16(Console.ReadLine());

            Console.WriteLine("Enter the 1-st average");
            CurrentAverage = float.Parse(Console.ReadLine());

            Console.WriteLine("Enter the 2-nd average");
            MissedAverage = Convert.ToDouble(Console.ReadLine());

            ArrayList newList = GetVariables(CurrentAverage, MissedAverage);
            List<int> OutputArray = newList.Cast<int>().ToList();

            Console.WriteLine("Marks to add :");

            for (int OutputCounter = 0; OutputCounter < OutputArray.Count; OutputCounter++)
            {
                Console.Write(OutputArray[OutputCounter] + " ");
            }
            Console.ReadKey();
        }
    }
}