【发布时间】:2019-08-24 04:46:26
【问题描述】:
我的目标是让用户通过将输入的黄金数量乘以 1000 来输入他们必须转换成现金的黄金数量。每个金条价值 1000 现金。然后我想显示总数是多少。
另外,是否有另一种方法可以使用更新功能来不断更新它?我觉得它的性能太密集了。
我得到这个错误:
FormatException:输入字符串的格式不正确 System.Int32.Parse (System.String s) (在 /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629) NSExchangeManager.ExchangeManager.Update () (在 Assets/ExchangeManager.cs:37)
我尝试交换 int.Parse() 的转换位置,但没有成功。
if(inputText.text != null)
{
// Get input text
string amountOfGold = inputText.text;
// Set gold value
int goldValue = 1000;
// Multiply goldvalue by amount of gold
int total = goldValue * int.Parse(amountOfGold);
// Show the total in the 'money text'
money.text = "$" + total.ToString();
// Show amount of gold typed
gold.text = amountOfGold;
}
【问题讨论】:
-
int.Parse(amountOfGold) 导致错误。因为文本值(可能是 string.Empty)无法转换为 int。更改 if 条件以使用
int.TryParse