当前位置:编程学习 > JAVA >>

再次求助 java array 移动法,

 这个是我的代码 我的问题在 move方程里面 谢谢大家帮忙

public class Slide{
    private char[][] cells;

    public Slide(){
        cells= new char[4][4];

    }

    public Slide(char[][] cells){
        for(char[] a: cells)
            for(char c: a)
                this.cells = cells;
        
    }

    public void print(){
        {
            for(char[] a: cells){
                for(char c: a){
                    System.out.print(c+" ");
                }

                System.out.println();            
            }

        }

    }

    public void clear(){

        for(char[] a : cells) {
            Arrays.fill(a, ' ');
        }

    }

    public void project(Slide other){       
       
        for(int row=0; row<other.cells.length; row++){
            for(int column = 0; column<other.cells[row].length; column++){
                if(cells[row][column]!= ' '){
                    other.cells[row][column] = cells[row][column];
                }
                
            }

        }
    }

    public void move(int row, int col){

      //这里是要移动数组cells横竖,,例如:
cells 本身是
'', '', '', '',
'', o, '', ''
'', o, '', ''
'', o, o, ''
被移动后是move(0,2)
'', '', 'o', '',
'', '', 'o', ''
'', '', 'o', 'o'
'', '', '', ''

slide.move(1,2)                                                       slide.move(1,2)
$ slide.cells -> {                                                      
slide.cells -> {
{ , , , },                                                           
{ , ,o, },                                                           
{ , ,o, },                                                           
{ , ,o,o}                                                            
}                                                                       
$ slide.move(1,1)                                                        slide.move(1,1)$ slide.cells -> {                                                    $ slide.cells -> {
{ , , , },                                                              
{ ,o, , },                                                              
{ ,o, , },                                                                                                                             { ,o,o, }
}                                                                        slide.move(0,0)                                                        slide.move(0,0)
$ slide.cells -> {                                                      

{o, , , },                                                           
{o, , , },                                                           
{o,o, , },                                                           
{ , , , }                                                            
}                                                                      
slide.move(1,0)                                                       slide.move(1,0)
slide.cells -> {                                                     
{ , , , },                                                             
{o, , , },                                                          
{o, , , },                                                           
{o,o, , }                                                            
}                                                                      
slide.move(0,1)                                                       slide.move(0,1)
slide.cells -> {                                                                                                                        >
{ ,o, , },                                                             
{ ,o, , },                                                              
{ ,o,o, },                                                           
{ , , , }                                                            
}                           

    }
}
java 移动 class slide --------------------编程问答-------------------- 楼主还是尽量的表达更清楚一些吧。。
   //这里是要移动数组cells横竖,,例如:
cells 本身是
'', '', '', '',
'', o, '', ''
'', o, '', ''
'', o, o, ''
被移动后是move(0,2)
'', '', 'o', '',
'', '', 'o', ''
'', '', 'o', 'o'
'', '', '', ''

这貌似并不是转移横竖。。 --------------------编程问答-------------------- 没看懂移动规则 --------------------编程问答-------------------- void move(int row, int col){
//这个代码是成功的是我想要的 但是
cells = new char[][]{{' ',' ',' ',' '},{' ','o',' ',' '},{' ','o',' ',' '},{' ','o','o',' '}};//这一步是错的 因为 cells不是我自己给的 是系统给的,而且这个值必须保持这个位置才可以
char[][] newCells = new char[][]{{' ',' ',' ',' '},{' ',' ',' ',' '},{' ',' ',' ',' '},{' ',' ',' ',' '}};
     for(int i=row;i<row+3;i++){
      for(int j=col;j<col+2;j++){
      newCells[i][j]=cells[i-row+1][j-col+1];
      }
     }
cells = newCells

}
补充:Java ,  Java相关
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,