java数组越界问题,求高人指点,问了好多人问题都没得到解决,希望能有高手指点迷津
String st = sc.next();
这句话得到的st并不是你需要的jla hhhhh.而是雀蔽棚jla
因为Scanner调用.next方法获取的到空格就终止了.所以st.split(” “)得到的只是一个数组长度为1的数组而已.
然后顷则c = compare(s[i][0],s[i][1]),当然就报数组下标越界了.以为没有s[i][1].
多用System.out.println(),在关键地方打印信息.比如数组长度,数组元素内容等并扰等.
java数组越界问题
temps.length()是你从控制台输入的字符串的长度,你的for循环誉谨里的条件错了,当然就报数组越界了!我给你改好了!
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“请输入庆绝基源程序:”宏哪);
String temps = sc.nextLine();
System.out.println();
String[] arrays = temps.split(” “);
for (int i = 0; i arrays.length; i ++)
{
if(arrays[i].equals(“begin”))
System.out.print(“(1,begin)”);
else
System.out.print(“(error)”);
}
}