0

特此附上我的代码供您参考。当我为乘法功能执行以下代码时,我收到以下错误,

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:choice2.main(choice2.java:99) 处的 -4

仅供参考 - 该程序适用于该程序中的所有其他功能。

代码 :

import java.util.*;

class choice2 {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        @SuppressWarnings("resource")
        Scanner S = new Scanner(System.in);

        int ch, subch;
        String Target = null;
        int codePos = 0, a, b, c = 0;

        String[] Arith = {"Add", "Sub"};
        String[] Mult = {"Multiplication", "Division", "Modulas" };

        System.out.println("1.Arithmatic");
        System.out.println("2.Mult/Div/Mod");
        System.out.println("0 to Exit from Menu");
        System.out.println("Enter your Choice");

        ch = S.nextInt();

        switch (ch) {
        case 1:
            System.out.println("1.Addition");
            System.out.println("2.Subtraction");
            System.out.println("9.Goback to Main Menu");
            subch = S.nextInt();

            if (subch == 1)
                Target = "Add";
            else
                Target = "Sub";

            codePos = Arrays.binarySearch(Arith, Target);

            switch (Arith[codePos]) {

            case "Add":
                System.out.println("Enter value for A");
                a = S.nextInt();
                System.out.println("Enter value for B");
                b = S.nextInt();

                c = a + b;

                System.out.println("The result is " + c);

                break;

            case "Sub":
                System.out.println("Enter value for A");
                a = S.nextInt();
                System.out.println("Enter value for B");
                b = S.nextInt();

                c = a - b;

                System.out.println("The result is " + c);

                break;
            }
            break;

        case 2:
            System.out.println("1.Multiplication");
            System.out.println("2.Division");
            System.out.println("3.Modulas");
            System.out.println("9.Goback to Main Menu");

            subch = S.nextInt();

            switch (subch) {

            case 1:
                Target = "Multiplication";
                break;

            case 2:
                Target = "Division";
                break;

            case 3:
                Target = "Modulas";
                break;

            default:
                System.out.println("Invalid Value");
            }

            System.out.println(codePos);
            System.out.println(Target);

//          codePos = Arrays.binarySearch(Mult, Target);
            codePos = Arrays.binarySearch(Mult, 0, Mult.length, Target);
            switch (Mult[codePos]) {

            case "Multiplication":
                System.out.println("Enter value for A");
                a = S.nextInt();
                System.out.println("Enter value for B");
                b = S.nextInt();

                c = a * b;

                System.out.println("The result is " + c);

                break;

            case "Division":
                System.out.println("Enter value for A");
                a = S.nextInt();
                System.out.println("Enter value for B");
                b = S.nextInt();

                c = a / b;

                System.out.println("The result is " + c);

                break;

            case "Modulas":
                System.out.println("Enter value for A");
                a = S.nextInt();
                System.out.println("Enter value for B");
                b = S.nextInt();

                c = a % b;

                System.out.println("The result is " + c);

                break;
            }
        }
        System.out.println("Exit from program");
    }
}
4

4 回答 4

3

Arrays.binarySearch如果找不到该值,则返回负数。此外,要使用二分搜索,必须对数组进行排序,但事实并非如此,这可能会导致您的错误。像这样声明你的数组:

String[] Mult = {"Division", "Modulas", "Multiplication" };
于 2013-07-15T17:54:06.230 回答
1

虽然需要对数组进行排序的其他答案是正确的,但他们错过了一点。考虑以下几行:

codePos = Arrays.binarySearch(Mult, 0, Mult.length, Target);
switch (Mult[codePos]) {
  case "Multiplication":
  .....
}

为什么要使用字符串 Target 搜索数组以获取索引,然后从匹配的数组中取回字符串?这基本上什么也没做(除了允许错误)。简单得多

switch (Target) {
  case "Multiplication":
   ...
  default: // handle any mistypings here...
}
于 2013-07-15T18:17:32.360 回答
1

线

switch (Mult[codePos]) {

由于输入数组未针对二分搜索进行排序,因此从上一次搜索中抛出ArrayIndexOutOfBoundsException原样codePos-4String

codePos = Arrays.binarySearch(Arith, Target);

文档

搜索键的索引,如果它包含在数组中;否则,(-(插入点)- 1)。插入点定义为将键插入数组的点:第一个元素的索引大于键,或 a.length

因此codePos成为

- (array.length + 1) = -4

二进制搜索值依赖于它们的值进行排序。因此你需要

String[] Mult = { "Division", "Modulas", "Multiplication", };
于 2013-07-15T17:54:32.507 回答
0

这里

 codePos = Arrays.binarySearch(Mult, 0, Mult.length, Target);// will retuen codePos as -4. 

所以你可以改变

 codePos = Arrays.binarySearch(Mult, 0, Mult.length, Target)

经过

            List<String>   list=Arrays.asList(Mult);
            codePos = list.indexOf(Target);
于 2013-07-15T18:09:47.827 回答