非静态字段需要对象引用

时间:2015-06-07 04:42:19

标签: c#

您好我创建了一个程序来计算3个浮点数的平均值并显示它们但是由于某种原因,当我尝试将值放入方法时,它会给出错误1的错误。非需要对象引用静态字段,方法或属性' average.Program.MeanAverageOfThree(float,float,float)'

任何帮助将不胜感激

static void Main(string[] args)
{
    float value1, value2, value3, average;

    Console.WriteLine("--Welcome to the Avarge Calculator--");
    Console.ReadLine();

    Console.WriteLine("Please Enter The First Number: ");
    value1 = float.Parse(Console.ReadLine());

    Console.WriteLine("Please Enter The Secound Number: ");
    value2 = float.Parse(Console.ReadLine());

    Console.WriteLine("Please Enter The Third Number: ");
    value3 = float.Parse(Console.ReadLine());

    average = MeanAverageOfThree(value1, value2, value3);

    Console.WriteLine("The Greatest Common Divisor of {0} and {1} and {2} is: {3} ", value1, value2, value3, average);
    Console.ReadLine();

}

public float MeanAverageOfThree(float value1, float value2, float value3) 
{
    float average;
    average = (value1 % 3 + value2 % 3 + value3 % 3 + 6) / 3 - 2 + (value1 / 3 + value2 / 3 + value3 / 3);


   return average;
}

1 个答案:

答案 0 :(得分:-1)

您处于静态类中,并尝试访问实例方法。您需要将MeanAverageOfThree方法设为静态。

static void Main(string[] args)
{
    float value1, value2, value3, average;

    Console.WriteLine("--Welcome to the Avarge Calculator--");
    Console.ReadLine();

    Console.WriteLine("Please Enter The First Number: ");
    value1 = float.Parse(Console.ReadLine());

    Console.WriteLine("Please Enter The Secound Number: ");
    value2 = float.Parse(Console.ReadLine());

    Console.WriteLine("Please Enter The Third Number: ");
    value3 = float.Parse(Console.ReadLine());

    average = Program.MeanAverageOfThree(value1, value2, value3);

    Console.WriteLine("The Greatest Common Divisor of {0} and {1} and {2} is: {3} ", value1, value2, value3, average);
    Console.ReadLine();
}

public static float MeanAverageOfThree(float value1, float value2, float value3) 
{
    return (value1 % 3 + value2 % 3 + value3 % 3 + 6) / 3 - 2 + (value1 / 3 + value2 / 3 + value3 / 3);
}