eclipse中没有出现错误,但是当我去运行代码时,应用程序没有任何反应。它分为三个等级。第一类包含 if 语句。我想知道我的问题是否在这里。
package course.infsci0017.lab04;
import java.util.Scanner;
import javax.swing.JFrame;
public class GameOfLife {
public static void main(String[] args) {
int testCase = 1;
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setTitle("Conway's Game of Life");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Cell[][] universe = new Cell[100][100];
for (int x = 0; x < universe.length-1; x++) {
for (int y = 0; y < universe[x].length-1; y++) {
universe[x][y] = new Cell();
}
}
if (testCase == 1) {
universe[3][2].calculateNext(3);
universe[3][2].updateCurrent();
universe[3][3].calculateNext(3);
universe[3][3].updateCurrent();
universe[3][4].calculateNext(3);
universe[3][4].updateCurrent();
} else if (testCase == 2) {
universe[49][50].calculateNext(3);
universe[49][50].updateCurrent();
universe[49][51].calculateNext(3);
universe[49][51].updateCurrent();
universe[50][49].calculateNext(3);
universe[50][49].updateCurrent();
universe[50][50].calculateNext(3);
universe[50][50].updateCurrent();
universe[51][50].calculateNext(3);
universe[51][50].updateCurrent();
} else {
}
UniverseComponent component = new UniverseComponent(universe);
frame.add(component);
frame.setVisible(true);
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while (input.length() == 0) {
int neighborCount = 0;
for (int x=1; x<universe.length-2; x++) {
for (int y=1; y<universe[x].length-2; y++) {
neighborCount = 0;
if (universe[x-1][y-1].isAlive()) {
neighborCount++;
}
if (universe[x-1][y].isAlive()) {
neighborCount++;
}
if (universe[x-1][y+1].isAlive()) {
neighborCount++;
}
if (universe[x][y-1].isAlive()) {
neighborCount++;
}
if (universe[x][y+1].isAlive()) {
neighborCount++;
}
if (universe[x+1][y-1].isAlive()) {
neighborCount++;
}
if (universe[x+1][y].isAlive()) {
neighborCount++;
}
if (universe[x+1][y+1].isAlive()) {
neighborCount++;
}
universe[x][y].calculateNext(neighborCount);
}
}
for (int x=1; x<universe.length-2; x++) {
for (int y=1; y<universe[x].length-2; y++) {
universe[x][y].updateCurrent();
}
}
component.repaint();
input = in.nextLine();
}
in.close();
}
}
2 级
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class UniverseComponent extends JComponent {
private Cell[][] universe;
public UniverseComponent(Cell[][] universe) {
super();
this.universe = universe;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
for (int x=0; x<universe.length-1; x++) {
for (int y=0; y<universe[x].length-1; y++) {
if (universe[x][y].isAlive()) {
g2.fillRect(x*5, y*5, 4, 4);
}
}
}
}
}
3 级
public class Cell {
private boolean current;
private boolean next;
public Cell() {
current = false;
next = false;
}
public Cell(int neighborCount) {
calculateNext(neighborCount);
updateCurrent();
}
public void calculateNext(int neighborCount) {
if (current) {
if ((neighborCount < 2) || (neighborCount > 3)) {
next = false;
} else {
next = true;
}
} else {
if (neighborCount == 3) {
next = true;
} else {
next = false;
}
}
}
public void updateCurrent() {
current = next;
}
public boolean isAlive() {
return current;
}
}
如果有人知道如何解决此问题,请告诉我。如有必要,请随意复制和粘贴代码。它不会让我发布图片,因为我没有足够的声誉。道歉