最好的解决方案是验证整数数量。
我在想以下几点。
double value = Double.Parse(txtCubes.Text);
double wholeValue = Math.Round(value);
Console.WriteLine("You added " + wholeValue + " blocks of ice");
是否有更好的原因确保输入整数值?
您可以使用比较验证器进行 int 验证
<asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Integer"
ControlToValidate="youTextBoxID" ErrorMessage="Value must be a whole number" />