-4

Hi I create a program to calculate the average of 3 float numbers and display them back but for some reason when I try to put the values into the method it gives the error of Error 1 An object reference is required for the non-static field, method, or property 'average.Program.MeanAverageOfThree(float, float, float)'

any help would be appreciated

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;
}
Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102
josh909
  • 13
  • 5

1 Answers1

-1

You are in a static class, and trying to access an instance method. You need to make your MeanAverageOfThree method static.

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);
}
Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102