Reputation: 51
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();
}
}
}
Upvotes: 2
Views: 92
Reputation: 29036
The method calcAge
is calling correctly with an integer value, and it will return an integer as well.
Two things you have to notice:
+
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)
Upvotes: 7