一个类里面怎样在一个方法里面调用另一个方法 C#

时间:2022-09-30 23:58:12
 class Program
    {
        static void Main(string[] args)
        {
            int[] arrI = new int[10];
            jisuan js = new jisuan();
            int i = 0;
            int j = 0;
            string strExit = "";
            Console.WriteLine("请输入十个数字:");
            while (true)
            {
                if (i == 10) break;
                Console.Write("请输入第"+(i+1).ToString() +"个数字:");
                string strRead = Console.ReadLine();
                if (!Int32.TryParse(strRead,out arrI[i]))
                {
                    Console.WriteLine("你输入的数字不正确!请重新输入");
                    continue;
                }
                Console.WriteLine("是否继续?(输入'N'退出)");
                strExit = Console.ReadLine();
                if (strExit == "N" || strExit=="n")
                {
                    break;
                }
                i++;
                j++;
            }
            while (true)
            {
                Console.WriteLine("请输入您要的操作:");
                Console.WriteLine("1、平均值");
                Console.WriteLine("2、和");
                Console.WriteLine("3、退出");
                string strRead1 = Console.ReadLine();
                if (strRead1 == "3")
                    break;
                switch (strRead1)
                {
                    case "1":
                        Console.WriteLine("平均值为:"+js.Average(arrI,j));
                        break;
                    case "2":
                        Console.WriteLine("和为:"+js.Sum(arrI));
                        break;


                }


            }
        }
    }
    class jisuan
    {
       
        public double Average(int[] arrI3,int j2)
        {
            double average = 0.0;
            //for (int i3 = 0; i3 < j2; i3++)
            //{
            //    average+=arrI3[i3];
            //}
            average = average / j2;
                return average;
        }
        public int Sum(int[] arrI4)
        {
            int sum = 0;
            for (int i4=0;i4<arrI4.Length;i4++)
            {
              sum+=arrI4[i4];
            }
                return sum;
        }

    }
类jisuan:
Average方法中注释的那个语句在下面Sum方法里已经出现过,现在我不想在Average这个方法里写这个语句,我想直接调用Sum方法,该怎么调用?希望高手指点一下,谢谢!!

5 个解决方案

#1


同一类中,直接调用啊
average=Sum(arrI3);

#2


public double Average(int[] arrI3,int j2)
  {
  double average = 0.0;
int sumResult = sum(arrI3);
  average = average / j2;
  return average;
  }

#3


public double Average(int[] arrI3,int j2)
  {
    return sum(arrI3)/ j2;
  }

#4


引用 3 楼 wxr0323 的回复:
C# code
public double Average(int[] arrI3,int j2)
  {
    return sum(arrI3)/ j2;
  }


+1

#5


用面向过程的思路写面向对象的代码,总是别扭

#1


同一类中,直接调用啊
average=Sum(arrI3);

#2


public double Average(int[] arrI3,int j2)
  {
  double average = 0.0;
int sumResult = sum(arrI3);
  average = average / j2;
  return average;
  }

#3


public double Average(int[] arrI3,int j2)
  {
    return sum(arrI3)/ j2;
  }

#4


引用 3 楼 wxr0323 的回复:
C# code
public double Average(int[] arrI3,int j2)
  {
    return sum(arrI3)/ j2;
  }


+1

#5


用面向过程的思路写面向对象的代码,总是别扭