-3
嗨,我正在學習過去幾天的java只有當我試圖執行一個簡單的程序,我得到了在運行時產生ArrayIndexOutOfBounds例外,請幫我如何解決數組索引越界異常
這裏修復這個異常是我的代碼
class Armstrong
{
public static void main(String args[])
{
if (args.length!=0) {
System.out.println("value is required");
System.exit(0);
}
int num = Integer.parseInt(args[0]);
int n = num;
int check =0, remainder;
while(num>0)
{
remainder = num%10;
check = check+(int)Math.pow(remainder,3);
num = num/10;
}
if (check==n)
System.out.println(n+ "isa armstrong number");
else
System.out.println(n + "is not a armstrong number");
}
}
可能重複[什麼導致java.lang.ArrayIndexOutOfBoundsException,我該如何防止它?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-如何做,我 - 預防它) – Atri
正如阿什託什提到看問題,並看看文件,瞭解如何以及爲什麼會發生。如果你想拋出錯誤,應該檢查參數長度爲零。如果參數長度不等於零,則當前正在拋出一個錯誤。 – Manglu