Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Am I unable to call a method with this style of User Input?

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;
        }
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading