【发布时间】:2019-04-04 16:44:39
【问题描述】:
现在,我将所有 3 个字符串都转换为一个 int,但是我在使用 Double.TryParse 方法转换每个字符串时遇到了麻烦。我想使用该方法而不是 int。
我尝试使用这种类型的代码 if (Double.TryParse(value, out number)),但我不确定这是否正确。
//Ask the user for height
Console.Write("Please enter the first part of your height in feet:");
string _height = Console.ReadLine();
int _heightVal = Int32.Parse(_height);
//Ask the user for inches
Console.Write("Please enter the second part of your height in inches:");
string _inches = Console.ReadLine();
int _inchesVal = Int32.Parse(_inches);
//Ask the user for pounds
Console.Write("Please enter your weight in pounds:");
string _pounds = Console.ReadLine();
int _poundsVal = Int32.Parse(_pounds);
【问题讨论】:
-
我看不到你在哪里使用
Double.TryParse。 -
tryparse应该包含在if中,您可以在其中以某种方式处理故障。 docs.microsoft.com/en-us/dotnet/api/… -
Double似乎是英尺和英寸的错误选择。也许是重量,但如果是小数,也许使用小数。