gpt4 book ai didi

compiler-errors - 嗨,我无法使此代码正常工作,因为它说 “not all code paths return a value.”,我不确定什么在这里不起作用

转载 作者:行者123 更新时间:2023-12-03 07:38:08 24 4
gpt4 key购买 nike

using System;

namespace code1
{
class Program
{
static object main()
{
Console.WriteLine("What's your name?");
string input = Console.ReadLine();
Console.WriteLine($"Hello {input}!");
/* This part will tell the user their age in a string */
Console.WriteLine("How old are you?");
double age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"You are {age} years old");
Console.WriteLine("Next Part....");
Console.WriteLine("Next.");


}
}

}

最佳答案

函数main的返回类型错误。它应该不返回任何内容=无效。

using System;

namespace code1
{
class Program
{
static void main()
{
Console.WriteLine("What's your name?");
string input = Console.ReadLine();
Console.WriteLine($"Hello {input}!");
/* This part will tell the user their age in a string */
Console.WriteLine("How old are you?");
double age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"You are {age} years old");
Console.WriteLine("Next Part....");
Console.WriteLine("Next.");


}
}

}
编辑2:
您还应该 与数据类型保持一致。您将 年龄定义为double ,但将 用户输入转换为Int32 要么使用 Convert.ToDouble() ,要么将变量声明更改为 int(或使用 var进行推断)。

关于compiler-errors - 嗨,我无法使此代码正常工作,因为它说 “not all code paths return a value.”,我不确定什么在这里不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65240016/

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