package bianyiyuanli;import java.util.Scanner;public class ZhengZe
{
public static void main(String args[])
{
int s[][] = new int [][]{{1,2},{3,2},{1,3},{3,3}};
boolean xh = true;
while(xh)
{
int panduan = 0;
int j;
System.out.println("请输入字符串:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char c[] = new char[str.length()];
for(int i=0; i<str.length(); i++)
{
c[i] = str.charAt(i);
}
for(int i=0; c[i] != '\0'; i++)  //提示这里出现指针越界异常?!
{
switch(c[i])
{
case 'a':
j = 0;
break;
case 'b':
j = 1;
break;
default: 
j = -1;
}
panduan = s[panduan][j];
}
if(panduan == 3)
{
System.out.println("Right!");
System.out.println();
}
else
{
System.out.println("Wrong!");
System.out.println();
}
}
}
}

解决方案 »

  1.   

    LZ收到C的思想干涉严重额
    JAVA中的String不是以'\0'结尾的呵。
    当然会越界咯
      

  2.   

    你自己的分支判断写的不是很好:
    switch(c[i])
    {
    case 'a':
    j = 0;
    break;
    case 'b':
    j = 1;
    break;
    default: 
    j = -1;
    }
    要是进入default,j=-1不报数组越界就怪了
    你可以这时候抛异常
      

  3.   

    同上。楼主还可以这么改:
    char c[] = str.toCharArray();
    for(int i=0;i<c.length;i++)
    {
      swich(c[i])
      …………
    }
    没必要自己动手将字符串拆成字符数组了
      

  4.   

    这个越界和那个没有关联,我上面j定义的是int,可以为-1的
      

  5.   


    你输入一个不带ab的字符串,s[][-1]这个二维数组不报错?