我尝试使用一种方法编写一个简单的程序来根据用户输入来计算年龄。但是当代码运行时,我得到了文本但没有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();
}
}
}
答案 0 :(得分:6)
方法calcAge
正在使用整数值正确调用,它也将返回整数。
你必须注意两件事:
+
来连接输出。调用这样的方法:
Console.WriteLine("Your age is :{0}", calcAge(Age));
或者像这样:
Console.WriteLine("Your age is :" + calcAge(Age));
或者像这样;
int currentAge=calcAge(Age);
Console.WriteLine("Your age is :{0}", currentAge)