英文:
Subtracting my number class (List of integers) is crashing my android app
问题
我创建了自己的数字类 https://pastebin.com/Hhp3CbB8
每当我执行 sub 函数时,似乎会导致我的应用程序崩溃。所有其他函数似乎都正常工作,只有在调用 sub 函数时应用程序崩溃。我遇到的大多数问题都有调试器的输出,并且应用程序在模拟器中关闭。但是在这次崩溃中,模拟器完全冻结,调试器停止而不打印任何内容。
英文:
I made my own number class https://pastebin.com/Hhp3CbB8
IdleNumb sub(IdleNumb? number) {
if (number != null) {
for (int i = number.numbs.length - 1; i >= 0; i--) {
while (numbs[i] < number.numbs[i]) {
numbs[i + 1] -= 1;
numbs[i] = numbs[i].toInt() + 94868329;
}
numbs[i] = numbs[i].toInt() - number.numbs[i].toInt();
fixNum();
}
}
return this;
}
whenever I perform the sub function it seems to crash my app. all the other functions seem to work fine it's only when ever sub is called that the app crashes. Most problems I have had, had a debugger printout and the app closes in the emulator. With this crash the emulator freezes up altogether and the debugger just stops without printing anything.
答案1
得分: 1
我不完全理解你对这个类想要什么,但错误出在这一行:
numbs[i + 1] -= 1;
如果你的结果是一个负数,那么它会抛出一个异常。
例如:
IdleNumb a=IdleNumb(6);
IdleNumb b=IdleNumb(7);
IdleNumb e=a.sub(b);
你尝试减少数组的第i+1个元素,但数组只有i个元素,所以你会得到一个RangeError。
英文:
I don't fully understand what you want with this class, but the error is in this line:
numbs[i + 1] -= 1;
If your result is a negative number then it throws an Exception
For example:
IdleNumb a=IdleNumb(6);
IdleNumb b=IdleNumb(7);
IdleNumb e=a.sub(b);
You try to decrement the array i+1 element but the array is only i long so you get a RangeError.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论