gpt4 book ai didi

c# - 为什么这不起作用?我没有得到预期的输出

转载 作者:行者123 更新时间:2023-11-30 19:54:57 24 4
gpt4 key购买 nike

我正在尝试编写一个简单的程序,使用一种方法根据用户输入计算年龄。但是当代码运行时,我得到了文本,但没有 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();

}
}
}

最佳答案

calcAge 方法使用整数值正确调用,它也会返回一个整数。

有两点需要注意:

  1. 您不会收到/显示从该调用方法返回的整数值。
  2. 您使用的显示语句格式不正确,您忘记/没有指定用于显示值的占位符。否则你必须使用 + 来连接输出。

像这样调用方法:

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)

关于c# - 为什么这不起作用?我没有得到预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40167589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com