java吧 关注:1,224,658贴子:12,682,970
  • 5回复贴,共1

跪求JAVA高手帮忙

取消只看楼主收藏回复

我写了一个井字棋的代码,现在写不下去了,不知道错在哪,调试时说发生了arrayindexoutofboundsexception这个错误,还是不明白,求高帮忙


IP属地:广东1楼2011-04-29 20:37回复
    sz[m][n+2].setIcon(image); flag=0; s[m][n+2]=2; } else if(s[m][n-1]==0 &&n-1>-1){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m][n-1].setIcon(image); flag=0; s[m][n-1]=2; } else aa(); } else if(s[m-1][n-1]==1 &&m-1>-1 &&n-1>-1){ if(s[m-2][n-2]==0 &&m-2>-1 &&n-2>-1){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m-2][n-2].setIcon(image); flag=0; s[m-2][n-2]=2; } else if(s[m+1][n+1]==0 &&m+1<3 &&n+1<3){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m+1][n+1].setIcon(image); flag=0; s[m+1][n+1]=2; } else aa(); } else if(s[m+1][n+1]==1 &&m+1<3 &&n+1<3){ if(s[m+2][n+2]==0 &&m+2<3 &&n+2<3) { ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m+2][n+2].setIcon(image); flag=0; s[m+2][n+2]=2; } else if(s[m-1][n-1]==0 &&m-1>-1 &&n-1>-1){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m-1][n-1].setIcon(image); flag=0; s[m-1][n-1]=2; } else aa(); } else if(s[m-1][n+1]==1 &&m-1>-1 &&n+1<3){ if(s[m-2][n+2]==0 &&m-2>-1 &&n+2<3) { ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m-2][n+2].setIcon(image); flag=0; s[m-2][n+2]=2; } else if(s[m+1][n-1]==0 &&m+1<3 &&n-1<-1){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m+1][n-1].setIcon(image); flag=0; s[m+1][n-1]=2; } else aa(); } else if(s[m+1][n-1]==1 &&m+1<3 &&n-1>-1){ if(s[m+2][n-2]==0 &&m+2<3 &&n-2>-1) { ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m+2][n-2].setIcon(image); flag=0; s[m+2][n-2]=2; } else if(s[m-1][n+1]==0 &&m-1>-1 &&n+1<3){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m-1][n+1].setIcon(image); flag=0; s[m-1][n+1]=2; } else aa(); } else aa(); } //else if(a==2) 结束 } //if(flag==1) 结束 } //else 结束 } public static int check(){ for(i=0;i<3;i++) for(j=0;j<3;j++) { if(s[i][j]==1 && s[i-1][j]==1 && s[i+1][j]==1&&i-1>-1 &&i+1<3) return 1; else if(s[i][j]==1 && s[1][j-1]==1 && s[i][j+1]==1 &&j-1>-1&&j+1<3) return 1; else if(s[i][j]==1 && s[i-1][j-1]==1 && s[i+1][j+1]==1 &&i-1>-1 &&i+1<3 &&j-1>-1 &&j+1<3) return 1; else if(s[i][j]==1 && s[i+1][j-1]==1 && s[i+1][j-1]==1 &&i-1>-1 &&i+1<3 &&j-1>-1 &&j+1<3) return 1; else if(s[i][j]==1 && s[i-1][j]==1 && s[i-2][j]==1 &&i-1>-1 &&i-2>-1) return 1; else if(s[i][j]==1 && s[i][j-1]==1 && s[i][j-2]==1 &&j-1>-1 && j-2>-1) return 1; else if(s[i][j]==1 && s[i-1][j-1]==1 && s[i-2][j-2]==1 &&i-1>-1&&i-2>-1 &&j-1>-1&& 


    IP属地:广东3楼2011-04-29 20:38
    回复
      j-2>-1) return 1; else if(s[i][j]==1 && s[i+1][j-1]==1 && s[i+2][j-2]==1 &&i+1<3&&i+2<3 &&j-1>-1&&j-2>-1) return 1; else if(s[i][j]==1 && s[i+1][j]==1 && s[i+2][j]==1 &&i+1<3&&i+2<3) return 1; else if(s[i][j]==1 && s[i][j+1]==1 && s[i][j+2]==1 &&j+1<3&&j+2<3) return 1; else if(s[i][j]==1 && s[i+1][j+1]==1 && s[i+2][j+2]==1 &&i+1<3&&i+2<3 &&j+1<3&&j+2<3) return 1; else if(s[i][j]==1 && s[i-1][j+1]==1 && s[i-2][j+2]==1 &&i-1>-1&&i-2>-1 &&j+1<3&&j+2<3) return 1; else if(s[i][j]==2 && s[i-1][j]==2 && s[i+1][j]==2 &&i-1>-1 &&i+1<3) return 2; else if(s[i][j]==2 && s[1][j-1]==2 && s[i][j+1]==2 &&j-1>-1&&j+1<3) return 2; else if(s[i][j]==2 && s[i-1][j-1]==2 && s[i+1][j+1]==2 &&i-1>-1 &&i+1<3 &&j-1>-1 &&j+1<3) return 2; else if(s[i][j]==2 && s[i+1][j-1]==2 && s[i+1][j-1]==2 &&i-1>-1 &&i+1<3 &&j-1>-1 &&j+1<3) return 2; else if(s[i][j]==2 && s[i-1][j]==2 && s[i-2][j]==2 &&i-1>-1 &&i-2>-1) return 2; else if(s[i][j]==2 && s[i][j-1]==2 && s[i][j-2]==2 &&j-1>-1 && j-2>-1) return 2; else if(s[i][j]==2 && s[i-1][j-1]==2 && s[i-2][j-2]==2 &&i-1>-1&&i-2>-1 &&j-1>-1&& j-2>-1) return 2; else if(s[i][j]==2 && s[i+1][j-1]==2 && s[i+2][j-2]==2 &&i+1<3&&i+2<3 &&j-1>-1&&j-2>-1) return 2; else if(s[i][j]==2 && s[i+1][j]==2 && s[i+2][j]==2 &&i+1<3 &&i+2<3) return 2; else if(s[i][j]==2 && s[i][j+1]==2 && s[i][j+2]==2 &&j+1<3 &&j+2<3) return 2; else if(s[i][j]==2 && s[i+1][j+1]==2 && s[i+2][j+2]==2 &&i+1<3&&i+2<3 &&j+1<3&&j+2<3) return 2; else if(s[i][j]==2 && s[i-1][j+1]==2 && s[i-2][j+2]==2 &&i-1>-1&&i-2>-1 &&j+1<3&&j+2<3) return 2; else return 0; } return 0; } public void aa(){ if(s[m][n]==1 &&m-1>-1 && s[m-1][n]==0){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m-1][n].setIcon(image); s[m-1][n]=2; flag=0; } else if(s[m][n]==1 &&n-1>-1 && s[m][n-1]==0){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m][n-1].setIcon(image); s[m][n-1]=2; flag=0; } else if(s[m][n]==1 &&n+1<3 && s[m][n+1]==0){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m][n+1].setIcon(image); s[m][n+1]=2; flag=0; } else if(s[m][n]==1 &&m+1<3 && s[m+1][n]==0){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m+1][n].setIcon(image); s[m+1][n]=2; flag=0; } else if(s[m][n]==1 &&n-1>-1 &&m-1>-1&& s[m-1][n-1]==0){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m-1][n-1].setIcon(image); s[m-1][n-1]=2; flag=0; } else if(s[m][n]==1 &&m+1<3&&n-1>-1 && s[m+1][n-1]==0){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m+1][n-1].setIcon(image); s[m+1][n-1]=2; flag=0; } else if(s[m][n]==1 &&m+1<3 && n+1<3&& s[m+1][n+1]==0){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m+1][n+1].setIcon(image); s[m+1][n+1]=2; flag=0; } else if(s[m][n]==1 && 


      IP属地:广东4楼2011-04-29 20:38
      回复
        m-1>-1&&n+1<3 && s[m-1][n+1]==0){ ImageIcon image = new ImageIcon("D://图/头文字D/2.PNG"); image.setImage(image.getImage().getScaledInstance(72,72,Image.SCALE_DEFAULT)); sz[m-1][n+1].setIcon(image); s[m-1][n+1]=2; flag=0; } } public static void main(String[] args) {    new J(); } public void mouseEntered(MouseEvent e) { // 鼠标进入到组件上时调用。 } public void mouseExited(MouseEvent e) { // 鼠标离开组件时调用。 } public void mousePressed(MouseEvent e) { // 鼠标按键在组件上按下时调用。 } public void mouseReleased(MouseEvent e) { // 鼠标按钮在组件上释放时调用。 } }
        


        IP属地:广东5楼2011-04-29 20:38
        回复
          有人么,复制上来代码太乱了


          IP属地:广东6楼2011-04-29 20:39
          回复
            那么乱,肯定沉的了...


            IP属地:广东7楼2011-04-29 20:40
            回复