我随函附上我的代码供您参考。当我执行下面的乘法功能代码时,我得到以下错误,
线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:-4 在choice2.main(choice2.java:99)
仅供参考 - 该计划适用于该计划中的所有其他功能。
代码:
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");
}
}
答案 0 :(得分:3)
Arrays.binarySearch
会返回一个负数。此外,要使用二进制搜索,必须对数组进行排序,而不是,这可能会导致您的错误。像这样声明你的数组:
String[] Mult = {"Division", "Modulas", "Multiplication" };
答案 1 :(得分:1)
该行
switch (Mult[codePos]) {
由于输入ArrayIndexOutOfBoundsException
数组没有为二进制搜索排序,从上一次搜索中抛出codePos
-4
为String
codePos = Arrays.binarySearch(Arith, Target);
来自docs
搜索键的索引,如果它包含在数组中;否则,( - (插入点) - 1)。插入点定义为键插入数组的点:第一个元素的索引大于键,或a.length
因此codePos
变为
- (array.length + 1) = -4
二进制搜索值依赖于它们的值进行排序。因此你需要
String[] Mult = { "Division", "Modulas", "Multiplication", };
答案 2 :(得分:1)
虽然需要对数组进行排序的其他答案是正确的,但它们错过了一个点。请考虑以下几点:
codePos = Arrays.binarySearch(Mult, 0, Mult.length, Target);
switch (Mult[codePos]) {
case "Multiplication":
.....
}
为什么要使用String,Target搜索数组以获取索引,然后从匹配的数组中获取String?这基本上完成了 nothing (除了允许bug)。更容易去
switch (Target) {
case "Multiplication":
...
default: // handle any mistypings here...
}
答案 3 :(得分: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);