如何添加get / set访问器?

时间:2015-03-10 00:58:39

标签: c# random get set

创建一个C#控制台应用程序,该应用程序使用重载方法计算2,3或4个整数的平均值。首先创建一个类Average。在此类中编写方法play以执行以下操作:

(1)生成随机整数以决定是否计算2,3或4个整数的平均值。 (2)生成2,3或4个随机整数。这些随机整数必须在1到1000的范围内。 (3)调用CalcAverage方法的三个版本之一来计算随机整数的平均值。 (4)在控制台窗口中显示随机整数及其平均值。

Average类应该有CalcAverage方法的三个版本,分别以2,3和4个整数作为参数。创建一个AverageTest类。在Main方法中,创建一个Average实例,然后调用其Play方法。

我到目前为止:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lab09_ex03
{
    public class Average
    {
        public int play();
        {
            Random randomNumbers = new Random();
            int numIntegers = randomNumbers.Next(2, 5);
            Console.WriteLine("For this round the program will display the average of\n {0} randomly generated integers", numIntegers);
            if (numIntegers == 2)
                {
                    int integer01 = randomNumbers.Next(1, 1001);
                    int integer02 = randomNumbers.Next(1, 1001);
                    return integer01, integer02;
                }
                if (numIntegers == 3)
                    {
                        int integer01 = randomNumbers.Next(1, 1001);
                        int integer02 = randomNumbers.Next(1, 1001);
                        int integer03 = randomNumbers.Next(1, 1001);
                        return integer01, integer02, integer03;
                    }
                else
                    {
                        int integer01 = randomNumbers.Next(1, 1001);
                        int integer02 = randomNumbers.Next(1, 1001);
                        int integer03 = randomNumbers.Next(1, 1001);
                        int integer04 = randomNumbers.Next(1, 1001);
                        return integer01, integer02, integer03, integer04;
                    }
        }
        public int CalcAverage(int integer01, int integer02)
        {
            int average01 = (integer01 + integer02)/2;
            Console.WriteLine("The average of {0} and {1} is {2}", integer01, integer02, average01);
        }
        public int CalcAverage(int integer01, int integer02, int integer03)
        {
            int average02 = (integer01 + integer02 + integer03)/3;
            Console.WriteLine("The average of {0} and {1} and {2} is {3}", integer01, integer02, integer03, average02);
        }
        public int CalcAverage(int integer01, int integer02, int integer03, int integer04)
        {
            int average03 = (integer01 + integer02 + integer03 + integer04)/4;
            Console.WriteLine("The average of {0} and {1} and {2} and {3} is {4}", integer01, integer02, integer03, integer04, average03);
        }

        static void Main(string[] args)
        {
            Play p = new Play();
            int integer01;
            int integer02;
            int integer03;
            int integer04;
            Console.WriteLine(p.Play());
            Console.WriteLine(p.CalcAverage(integer01, integer02));
            Console.WriteLine(p.CalcAverage(integer01, integer02, integer03));
            Console.WriteLine(p.CalcAverage(integer01, integer02, integer03, integer04));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,play是一种方法,因此需要public int Play()。 (除非这是你试图使用get / set访问的内容?如果是这样的话,它根本就不是好的约定。) 而且我不确定我理解你在这里提出的问题。如果你想要获得和设置方法的东西,它就是这样的:

private int myPrivateValue = 0;
public int myPublicValue
{
    get
    {
        return myPrivateValue;
    }
    set
    {
        myPrivateValue = value;
    }
}

您可以在官方Microsoft参考网站上阅读:https://msdn.microsoft.com/en-us/library/aa287786%28v=vs.71%29.aspx

编辑:get / set之类的目的是当你有多个类来验证输入,记录它的变化等等...你有你的公共价值,这就像其他类的方式一样对您的私人领域的访问权限有限。