I've just recently started my intermediate course in C# programming and I'm learning how to create multiple classes and creating methods to use in my program.
It's a very new topic to me so I apologize if it's something very obvious or stupid. I am getting below message in all of my methods:
Cannot access static method in non-static context
Code in method class:
public static int Add(params int[] numbers) {
var sum = 0;
foreach (var n in numbers) {
sum += n;
}
return sum;
}
public static int Subtract(params int[] numbers) {
var sum = 0;
foreach (var n in numbers) {
sum -= n;
}
return sum;
}
public static int Multiply(params int[] numbers) {
var sum = 0;
foreach (var n in numbers) {
sum *= n;
}
return sum;
}
public static int Divide(params int[] numbers) {
var sum = 0;
foreach (var n in numbers) {
sum /= n;
}
return sum;
}
public static string[] CheckingOfSomeSort(string userInput, int value, bool isAddition, bool isSubtraction, bool isDivision, bool isMultiplication) {
if (userInput.Contains("+")) {
var addition = userInput.Split('+');
value = 1;
isAddition = true;
return addition;
} else if (userInput.Contains("-")) {
var subtraction = userInput.Split('-');
value = 2;
isSubtraction = true;
return subtraction;
} else if (userInput.Contains("*")) {
var multiplication = userInput.Split('*');
value = 3;
isMultiplication = true;
return multiplication;
} else if (userInput.Contains("/")) {
var division = userInput.Split('/');
value = 4;
isDivision = true;
return division;
}
return null;
}
I am attempting to create a calculator (which I have already done, however I'm trying to use methods now)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…