简体   繁体   中英

Why isn't this working? I'm not getting the expected output

I'm trying to write a simple program using a method to calculate age from user input. But when the code is ran I get the text but no integer result for Age.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharpExercises
{
    class Program
    {            
        public static int calcAge (int yourAge) {
            int currentYear;
            int year = 2016;
            currentYear = year - yourAge;
            return currentYear;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the year you were born in: ");
            int Age = int.Parse(Console.ReadLine());
            calcAge(Age);
            Console.WriteLine("Your age is : ", Age);
            Console.ReadKey();

        }
    }
}

The method calcAge is calling correctly with an integer value, and it will return an integer as well.

Two things you have to notice:

  1. You don't receive/Displaying the integer value returning from that calling method.
  2. The display statement that you are using is not well-formatted, you forgot/have not specified the placeholder for displaying the value. or else you have to use a + for concatenate the output.

Call the method like this:

Console.WriteLine("Your age is :{0}", calcAge(Age));

or like this :

Console.WriteLine("Your age is :" + calcAge(Age));

or like this;

int currentAge=calcAge(Age);
Console.WriteLine("Your age is :{0}", currentAge)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM