I am trying to call a method with the inputs stated within Main. I have seen that some people get user inputs from another method besides Main but wanted to complete it this way. Is it possible?
I am able to input data when asked as stated. Although, "Your answer is: " returns nothing.
static void Main(string[] args)
{
int number1, number2;
Console.WriteLine("Please enter a number: ");
number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter another number: ");
number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your answer is: ", Add(number1, number2));
Console.ReadLine();
}
public static int Add(int num1, int num2)
{
return num1 + num2;
}
}
}
>Solution :
The problem is you don’t have any string format tokens in your text Your answer is:
. Try adding {0}
like this: Your answer is: {0}
or maybe even {0} is your answer.
See https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-8.0#system-console-writeline(system-string-system-object) for more information.