我无法使此代码正常工作

我无法使此代码正常工作

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/

10-08 20:55