c# - 如何修复最佳重载方法匹配有一些无效参数?

标签 c#

我在标题中收到错误,代码有什么问题?我认为这是一个语法错误,但我不确定,因为我没有太多关于错误实际含义的信息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{

class Program
{

    static void Main(string[] args)
    {

        Console.WriteLine("Please Input Number of Rows you want to make in your pyrimid: ");

        int num = int.Parse(Console.Read()); // error here

        Console.WriteLine(num);// Just to check if it is getting the right number
        Console.Read();//This is Here just so the console window doesn't close when the program runs

    }
}
}

编辑:

澄清一下,我希望代码只是从用户那里获取一个数字,然后打印用户输入的数字。

最佳答案

int.Parse 接受一个字符串作为参数。使用 Console.ReadLine() 从用户获取字符串,然后将其传递给 int.Parse

int num = int.Parse(Console.ReadLine());

请注意,如果用户输入无法识别为 int 的内容,这将抛出 FormatException。如果您不确定用户是否会输入一个正确的数字(我总是不知道),请使用 TryParse。下面给出示例

int value;

if (int.TryParse(Console.ReadLine(), out value))
    Console.WriteLine("parsed number as: {0}", value);
else
    Console.WriteLine("incorrect number format");

关于c# - 如何修复最佳重载方法匹配有一些无效参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15703737/

相关文章:

c# - 强制 Entity Framework 在一次往返中更新多个记录。

c# - 为什么 WinRT MediaElement.SetSource 不执行它应该执行的操作?

c# - 从属性中获取属性的名称

c# - 绑定(bind)对象 DataGridView C#

c# - 使用 Xbox Controller 播放动画

c# - 我可以从 64 位应用程序以 32 位运行 C# 程序集 (dll) 吗?

c# - 奥尔良用极简的用例慢

c# - 导致 Unity 崩溃的构造函数

c# - 如何使用 Razor 在主视图 View 模型中保存部分 View View 模型项?

c# - FluentValidation 涉及正在验证的对象中的 2 个属性