int 的最佳重载方法匹配.Parse(string)' 有一些无效的参数.这是什么意思
本文关键字:无效 参数 意思 是什么 方法 重载 最佳 Parse int string | 更新日期: 2023-09-27 18:33:19
已经尝试了 4 个多小时了,我丝毫不是专家,我绝对需要这方面的帮助。知道出了什么问题吗?
// Declare variables
int inches = 0;
double centimetres = 0;
string input;
//Ask for input
Console.Write("Enter Number of centimetres to be converted: ");
input = Console.ReadLine();
//Convert string to int
centimetres = double.Parse(input);
inches = int.Parse(input);
inches = int.Parse(centimetres / 2.54);
//Output result
Console.WriteLine("Inches = " + inches + "inches.");
}
}
"转换"inches = int.Parse(centimetres / 2.54);
毫无意义。 int.Parse
用于将表示数字的string
转换为int
。但是你把它传了一double
.
要使其正常工作,您的代码需要如下所示:
//Ask for input
Console.Write("Enter Number of centimetres to be converted: ");
double input = Console.ReadLine();
//Convert string to int
double centimetres = double.Parse(input);
double inches = centimetres / 2.54;
//Output result
Console.WriteLine("Inches = " + inches + "inches.");
几点:
- 在使用时声明变量,而不是在方法的开头声明变量。这是旧语言的遗留问题,需要首先定义变量。
- 完全删除
inches = int.Parse(input);
,因为永远不会使用结果,因为它会在下一行中被覆盖。 - 将
inches
声明为double
而不是int
。否则,您将无法拥有小数英寸。 - 只需将除法结果分配给
inches
即可。这里不需要解析。
问题最像这一行:英寸 = 整数。解析(厘米/2.54);
国际。Parse 需要一根字符串,厘米/2.54 是双精度。要将双精度转换为 int,请改用 Convert.ToInt32。
centimetres / 2.54
的结果是双精度,也没有重载接受双精度作为int中的参数。解析