我一直在嘗試撰寫一段代碼,它需要輸入金錢并用數字(1000 到 1000、1,000,000 到 100 萬等)重寫它。到目前為止,我還無法通過 Unity 告訴我在我的陣列崩潰之前有一個堆疊溢位,但我不明白它為什么會溢位。我錯過了一些巨大的東西還是這里不合適?Unity 一直給我錯誤“請求的操作導致堆疊溢位,MoneyTruncate() (at Assets/Scripts/Money.cs:60”),這是與此 void 中的陣列有關的行。
{
string[] Numerals = new string[]{" ", "thousand", "million", "billion"} ;
int i = 0;
TotalMoneyFloat = (TotalMoney / (10 ^ (i * 3)));
TotalMoneyFloatLimit = (TotalMoney / (10 ^ ((i 1) * 3)));
//current iteration of Numeral is correct- greater than current numeral, less than next
if(TotalMoneyFloat >= 1 && TotalMoneyFloatLimit < 1)
{
TotalMoneyText.GetComponent<Text>().text = "$" TotalMoneyFloat.ToString("0.00") " " Numerals[i];
}
//current iteration of Numeral is too high- less than current numeral
if(TotalMoneyFloat < 1)
{
i--;
MoneyTruncate();
}
//current iteration of Numeral is too low- greater than current numeral
if(TotalMoneyFloatLimit >= 1)
{
i ;
MoneyTruncate();
}
//i is at its limit for number of numeral available- i has reached max value for the array but money is higher than
if(i > 3 && TotalMoneyFloatLimit >= 1)
{
TotalMoneyText.GetComponent<Text>().text = "$" TotalMoneyFloat.ToString("0.00") " " Numerals[i];
}
}```
uj5u.com熱心網友回復:
哪一行是“來自陣列的行”?這是什么功能?如果我不得不猜測,你在這里的某個地方有一個回圈參考,如果呼叫這個函式就會發生這種情況MoneyTruncate()
。
邏輯沒有做你認為它正在做的事情,我會敦促你設定一個斷點并進入每個函式。在某些時候,您會看到代碼中的同一點不斷出現。
我敢打賭這個函式被命名為 MoneyTruncate 并且你試圖遞回呼叫它,但是你的遞回被破壞了 - 你的i
變數是 LOCAL 并且遞回之前的任何遞減都不會影響被呼叫的子/重復實體。這意味著重復實體遵循相同的步驟,以相同的方式呼叫相同的函式,并且這種情況一直持續到您的堆疊構建了太多函式呼叫而溢位。
您正在使用遞回來解決實際上不需要遞回的問題。只需檢查是否 >= 1e12 并回傳萬億,1e9 為十億,等等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/488910.html
上一篇:OpenURL在編輯器和運行時作業,但在構建到Android后無法作業
下一篇:剛體在Unity中穿過物體