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