Search code examples
c#methodssubtraction

Trouble with method subtraction c#


I am experiencing some difficulty with method. My code seems to work just fine until I reach my formula for method. I have done something similar I'm not seeing what I'm doing wrong. It is the portion that has yearsToWork.

int age;
int yearsToWork;

Console.Write("Enter your name:");
string name = Console.ReadLine();

Console.Write("Enter your age:");
age = Convert.ToInt32(Console.ReadLine());

yearsToWork = 65 - age;
Console.Write("\nYou will work:", yearsToWork);
Console.Write("years before you retire.");
Console.Read();

Thank-you for the assistance.


Solution

  • This should work:

        static void Main(string[] args)
        {
            int age;
            int YearsToWork;
    
            Console.Write("Enter your name:");
            string name = Console.ReadLine();
            Console.Write("Enter your age:");
            age = Convert.ToInt32(Console.ReadLine());
            YearsToWork = 65 - age;
            Console.WriteLine("You will work: {0} years before you retire", YearsToWork);
            Console.Read();
        }
    

    Console.WriteLine(); work similar to string.Format(); when inserting a string;
    The 1st parameter after the string will be {0}, second string {1} and so on...