Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java和条件的问题(生命的游戏)_Java_Arrays - Fatal编程技术网

java和条件的问题(生命的游戏)

java和条件的问题(生命的游戏),java,arrays,Java,Arrays,我试图用java实现生活游戏,作为学习这种语言的练习 不幸的是,我有一个问题,因为我似乎无法使这个程序正常运行 我实现了一个torodial和(平面是一个甜甜圈),没有问题: int SumNeighbours (int i, int j) { int value = 0; value = world[( i - 1 + row ) % row][( j - 1 + column ) % column]+world[( i - 1 + row ) % row][j]+

我试图用java实现生活游戏,作为学习这种语言的练习

不幸的是,我有一个问题,因为我似乎无法使这个程序正常运行

我实现了一个torodial和(平面是一个甜甜圈),没有问题:

    int SumNeighbours (int i, int j) {

  int value = 0;


  value = world[( i - 1 + row  ) % row][( j - 1 + column  ) % column]+world[( i - 1 + row  ) % row][j]+world[( i - 1 + row  ) % row][( j + 1 ) % column];

  value = value + world[i][( j - 1 + column  ) % column] + world[i][( j + 1 ) % column];

  value = value + world[( i + 1 ) % row][( j - 1 + column  ) % column] + world[( i + 1 ) % row][j]+world[ ( i+1 ) % row ][( j + 1 ) % column];


  return value;
 }
当我测试它时,它的计算是正确的:

 void NextWorldTest () {

  int count;

  int [][] nextWorld = new int[row][row];

  nextWorld = world;


  for (int i=0; i<row; i++) {
   for (int j=0; j<column; j++) {

    count = SumNeighbours(i,j);

    System.out.print(" " + count + " ");       


   }

   System.out.println();

  }

  world=nextWorld;



 }
void NextWorldTest(){
整数计数;
int[][]nextWorld=新的int[行][行];
nextWorld=世界;

对于(inti=0;i我认为,问题在于数组赋值。你们在这里做什么

int [][] nextWorld = new int[row][row];
nextWorld = world;
您应该创建新的数组
[row][column]
,然后根据来自world的数据填充它

还有更多

  • 在Java中,可以执行smt,如
    anArray.length
    anArray[0].length
    ,因此不需要行和列变量
  • 您应该以小写字母命名所有方法,如
    sumnights()
    而不是
    sumnights()

upd:第二个建议中有打字错误:已修复

如果回答a)会容易得多你格式化了你的代码;b)你给出了一个错误的简短示例。这里的代码比你需要的要多得多。我确定。我重新格式化了…StackOverflow编辑器有问题…条件运算符标记在那里,因为我添加ifs时代码中断了…我认为条件a没有任何问题l运算符。所以我删除了这个标记。是的,这部分是错误的,但这不是问题,因为数组是正方形的。……thanx寻求帮助:)@Muad'Dib,你认为行
nextWorld=world;
?它在nextWorld中复制world,这样我就可以修改它的值,但仍然有一个原始的引用来进行求和……我明白了吗g?@Muad'Dib,实际上,你根本不需要做任何这样的复制/赋值。只需创建空的nextWorld,填充它(就像你做的那样),然后进行赋值(就像你做的那样)。非常感谢你……你解决了它……为什么这么小的一行会破坏这么多代码?我是按值复制的,不是吗?是按引用复制的吗?
    package com.GaOL;

public class GameWorld {

 int [][] world;

 int row;

 int column;

 public int GetRow() {

  return row;
 }

 public int GetColumn() {

  return column;
 }

 public int  GetWorld (int i, int j) {


  return world[i][j];

 }

 void RandomGen (int size, double p1) {

  double randomCell;

  row = size;
  column = size;

  world = new int[row][column];

  for (int i = 0; i<row; i++ ) {
   for (int j = 0; j<column; j++ ) {

    randomCell=Math.random();

    if (randomCell < 1-p1) {

     world[i][j] =  0;

    } else {

     world[i][j] =  1;

    }



   }

  }

 }

 void printToConsole() {

  double test = 0; 

  for (int i=0; i<row; i++) {
   for (int j=0; j<column; j++) {

    if ( world[i][j] == 0 ) {

     System.out.print("   ");

    } else {
     System.out.print(" * ");

     test++;

    }

   }

   System.out.println("");  

  }


  System.out.println("ratio is " + test/(row*column));


}

 int SumNeighbours (int i, int j) {

  int value = 0;


  value = world[( i - 1 + row  ) % row][( j - 1 + column  ) % column]+world[( i - 1 + row  ) % row][j]+world[( i - 1 + row  ) % row][( j + 1 ) % column];

  value = value + world[i][( j - 1 + column  ) % column] + world[i][( j + 1 ) % column];

  value = value + world[( i + 1 ) % row][( j - 1 + column  ) % column] + world[( i + 1 ) % row][j]+world[ ( i+1 ) % row ][( j + 1 ) % column];


  return value;
 }

 void NextWorldTest () {

  int count;

  int [][] nextWorld = new int[row][row];

  nextWorld = world;


  for (int i=0; i<row; i++) {
   for (int j=0; j<column; j++) {

    count = SumNeighbours(i,j);

    System.out.print(" " + count + " ");       


   }

   System.out.println();

  }

  world=nextWorld;



 }


 void NextWorld () {

  int count;

  int [][] nextWorld = new int[row][column];

  nextWorld = world;


  for (int i=0; i<row; i++) {
   for (int j=0; j<column; j++) {

    count = SumNeighbours(i,j);

    System.out.print(" " + count + " ");


    if (  ( world[i][j] == 0) &&  ( count == 3 )  ) {

     nextWorld[i][j] = 1;

    } else if ( ( world[i][j] == 1 ) && ( (count == 3) || (count == 2) )) {

     nextWorld[i][j] = 1;

    } else {

     nextWorld[i][j]=0;

    }


   }

   System.out.println();

  }

  world=nextWorld;



 }

}
    package com.GaOL;

public class GameTestClass {


 public static void main(String[] args) {

  GameWorld prova = new GameWorld();

  prova.RandomGen(10, 0.02);

  for (int i=0; i<3; i++) {

   prova.printToConsole();

   prova.NextWorld();

  }



 }

}
int [][] nextWorld = new int[row][row];
nextWorld = world;