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