使此Java代码更有效

时间:2017-07-01 05:16:18

标签: java arrays algorithm for-loop numbers

此代码正常工作,但我的代码执行后最终出现错误,即线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:-1     在AddNum.main(AddNum.java:24),任何人都可以修复此问题或使其更有效使用。

import java.util.Scanner;
public class AddNum {
public static void main(String args[])
{
    int num1,num2,sum,i=0;
    int[] arr =new int[5];
    System.out.println("Enter Any two number to Add and See magic");
    Scanner scan = new Scanner(System.in);
    num1 = scan.nextInt();
    num2 = scan.nextInt();
    sum = num1 + num2;
    System.out.println("The Sum of "+num1+" and "+num2+" is = "+sum );
    do
    {
        arr[i++]=sum%10;
        sum/=10;
    }while(sum>0);
    for(int p=i;p>=0;p--)
    {
        mag(arr[p-1]);
    }
}
public static void mag(int sum)
{
    switch(sum){
        case 0:
            System.out.println("    00000");
            System.out.println("   00   00");
            System.out.println("  000   000");
            System.out.println("  000   000");
            System.out.println("  000   000");
            System.out.println("  000   000");
            System.out.println("  000   000");
            System.out.println("   00   00");
            System.out.println("    00000");
            break;

        case 1:
        System.out.println("      11");
        System.out.println("     111");
        System.out.println("   1 111");
        System.out.println("     111");
        System.out.println("     111");
        System.out.println("     111");
        System.out.println("     111");
        System.out.println("     111");
        System.out.println("    11111");
        break;
        case 2:
            System.out.println("    2222");
            System.out.println("  22    22");
            System.out.println("  2     222");
            System.out.println("        22");
            System.out.println("       22");
            System.out.println("     22");
            System.out.println("   222    2");
            System.out.println("  222    22");
            System.out.println("  222222222");
            break;
        case 3:
            System.out.println("    3333");
            System.out.println("  33   333");
            System.out.println("  3     333");
            System.out.println("       33");
            System.out.println("     333");
            System.out.println("       33");
            System.out.println("  3     333");
            System.out.println("  33   333");
            System.out.println("    3333");
            break;
        case 4:
            System.out.println("        44");
            System.out.println("      4444");
            System.out.println("     4 444");
            System.out.println("    4  444  ");
            System.out.println("   4   444");
            System.out.println("  4444444444");
            System.out.println("       444  ");
            System.out.println("       444");
            System.out.println("      44444");
            break;
        case 5:
            System.out.println("  5555555");
            System.out.println("  555    5");
            System.out.println("  555");
            System.out.println("  555");
            System.out.println("  555555");
            System.out.println("       555");
            System.out.println("  5     555");
            System.out.println("  55   555");
            System.out.println("    5555");
            break;
        case 6:
            System.out.println("    66666");
            System.out.println("   66    66 ");
            System.out.println("  666     6");
            System.out.println("  666");
            System.out.println("  6666666");
            System.out.println("  666   666");
            System.out.println("  666    666");
            System.out.println("   66   666");
            System.out.println("    66666");
            break;
        case 7:
            System.out.println("   777777777");
            System.out.println("  77     777");
            System.out.println("  7     777");
            System.out.println("       77");
            System.out.println("      77");
            System.out.println("     77");
            System.out.println("    777");
            System.out.println("   777");
            System.out.println("   777");
            break;
        case 8:
            System.out.println("    888888");
            System.out.println("   888  888");
            System.out.println("   888  888");
            System.out.println("    888888");
            System.out.println("   888  888");
            System.out.println("  888    888");
            System.out.println("  888    888");
            System.out.println("   888  888");
            System.out.println("     8888");
            break;
        case 9:
            System.out.println("    99999");
            System.out.println("   999 999");
            System.out.println("  999   999");
            System.out.println("  999   999");
            System.out.println("    9999999");
            System.out.println("        999");
            System.out.println("  9     999");
            System.out.println("  99   999");
            System.out.println("    9999");
            break;





    }
} }

1 个答案:

答案 0 :(得分:0)

您正在获取ArrayIndexOutOfBounds,因为您正在访问数组中不存在的数组元素,所以您需要做的是获取用户输入并计算它的总和并将其转换为数组,以便您可以通过替换这段代码来做到这一点: -

 do
    {
        arr[i++]=sum%10;
        sum/=10;
    }while(sum>0);
    for(int p=i;p>=0;p--)
    {
        mag(arr[p-1]);
    }

通过以下代码: -

 int[] arr = new int[String.valueOf(sum).length()];
    for(int p=0;p<arr.length;p++)
    {
        mag(arr[p]);
    }

所以最终的程序看起来像这样: -