我的人生游戏程序的文件读取器有问题 包edu.bsu.cs121.mamurphy; 导入java.util.*; 导入java.io.*; 导入javax.swing.*; 导入java.awt.*; //莫里斯·墨菲 //CS121 // 10/17/15 公共类GameOfLifeMain扩展了JFrame{ //世界的初步阅读和印刷 公共游戏{ 超级(“生命游戏”); 设置大小(600445); JPanel面板=新的JPanel(); panel.setLayout(新网格布局(1,2)); test1=新测试(); 试验1.立根背景(颜色:浅灰色); panel.add(test1); 设置内容窗格(面板); setVisible(真); 温度1=新温度(测试1); 一、启动(); } 公共静态void main(字符串[]asd){ 干扰素的新配子(); System.out.println(); } } 类Temp扩展线程{ 测试动画; 公共温度(测试动画){ this.anim=anim; } public void run()//将对每个test begin实例执行 { anim.begin(); } } 类测试扩展了JPanel { 最终静态int numOfRow=25,numOfCol=75; 最终静态字符点='; 静态字符[][]网格=新字符[numOfRow+2][numOfCol+2]; 静态字符[][]nextgrid=新字符[numOfRow+2][numOfCol+2]; 布尔同型滞后; 布尔空白标志; 公共静态void init(char[]]grid,char[]]nextgrid){ 对于(int numorrow=0;numorrow

我的人生游戏程序的文件读取器有问题 包edu.bsu.cs121.mamurphy; 导入java.util.*; 导入java.io.*; 导入javax.swing.*; 导入java.awt.*; //莫里斯·墨菲 //CS121 // 10/17/15 公共类GameOfLifeMain扩展了JFrame{ //世界的初步阅读和印刷 公共游戏{ 超级(“生命游戏”); 设置大小(600445); JPanel面板=新的JPanel(); panel.setLayout(新网格布局(1,2)); test1=新测试(); 试验1.立根背景(颜色:浅灰色); panel.add(test1); 设置内容窗格(面板); setVisible(真); 温度1=新温度(测试1); 一、启动(); } 公共静态void main(字符串[]asd){ 干扰素的新配子(); System.out.println(); } } 类Temp扩展线程{ 测试动画; 公共温度(测试动画){ this.anim=anim; } public void run()//将对每个test begin实例执行 { anim.begin(); } } 类测试扩展了JPanel { 最终静态int numOfRow=25,numOfCol=75; 最终静态字符点='; 静态字符[][]网格=新字符[numOfRow+2][numOfCol+2]; 静态字符[][]nextgrid=新字符[numOfRow+2][numOfCol+2]; 布尔同型滞后; 布尔空白标志; 公共静态void init(char[]]grid,char[]]nextgrid){ 对于(int numorrow=0;numorrow,java,conways-game-of-life,Java,Conways Game Of Life,您可能需要提供文件的完整路径。这意味着文件在系统中的确切位置 此外,将文件作为资源放在项目中会更容易 package edu.bsu.cs121.mamurphy; import java.util.*; import java.io.*; import javax.swing.*; import java.awt.*; // Maurice Murphy // CS121 // 10/17/15 public class GameOfLifeMain e

您可能需要提供文件的完整路径。这意味着文件在系统中的确切位置

此外,将文件作为资源放在项目中会更容易

 package edu.bsu.cs121.mamurphy;

    import java.util.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;

// Maurice Murphy
// CS121
// 10/17/15

public class GameOfLifeMain extends JFrame {

    // Intitial reading and printing of the world

    public GameOfLifeMain() {
        super("Game of Life");
        setSize(600, 445);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));
        Test test1 = new Test();
        test1.setBackground(Color.LIGHT_GRAY);
        panel.add(test1);
        setContentPane(panel);
        setVisible(true);
        Temp one = new Temp(test1);
        one.start();
    }

    public static void main(String[] asd) {
        new GameOfLifeMain();
        System.out.println();

    }
}

class Temp extends Thread {
    Test anim;

    public Temp(Test anim) {
        this.anim = anim;

    }

    public void run()// for each instance of test begin will be executed
    {
        anim.begin();
    }
}

class Test extends JPanel

{
    final static int numOfRow = 25, numOfCol = 75;
    final static char DOT = '.';
    static char[][] grid = new char[numOfRow + 2][numOfCol + 2];
    static char[][] nextgrid = new char[numOfRow + 2][numOfCol + 2];
    boolean sameFlag;
    boolean blankFlag;

    public static void init(char[][] grid, char[][] nextgrid) {
        for (int numOfRow = 0; numOfRow <= numOfRow + 1; numOfRow++) {
            for (int numOfCol = 0; numOfCol <= numOfCol + 1; numOfCol++) {
                grid[numOfRow][numOfCol] = DOT;
                nextgrid[numOfRow][numOfCol] = DOT;
            }
        }
    }

    public static void pause() {
        try {
            Thread.currentThread();
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
        }
    }

    public void begin() {
        init(grid, nextgrid);
        read(grid);
        repaint(); // calls paintComponent
        pause();
        while (sameFlag == true && blankFlag == false) {
            nextGen(grid, nextgrid);
        }
    }

    public static void read(char[][] grid) {
        Scanner world = new Scanner(System.in);
        System.out.println("Type the file name of the world you'd like to create.");
        String fileName = world.nextLine();
        {
            try {
                world = new Scanner(new File(fileName));
            } catch (Exception ex) {
                System.out.println("Please insert a valid file name.");

            }
            ;

            for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
                String s = world.next();
                for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                    grid[numOfRow][numOfCol] = s.charAt(numOfCol - 1);
                }

            }
        }
    }

    public void print(Graphics g) {
        int x, y;
        y = 20;
        for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
            x = 20;
            for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                g.drawString("" + grid[numOfRow][numOfCol], x, y);
                x = x + 7;
            }
            y = y + 15;
        }
    }

    public static int neighbors(char[][] grid, int r, int c) {
        // counts the # of closest neighbors that are X's
        int count = 0;
        for (int numOfRow = r - 1; numOfRow <= r + 1; numOfRow++) {
            for (int numOfCol = c - 1; numOfCol <= c + 1; numOfCol++) {
                if (grid[numOfRow][numOfCol] == 'X') {
                    count++;
                }
            }
        }
        if (grid[r][c] == 'X') {
            count = count - 1;
        }
        return count;
    }

    public static void nextGen(char[][] grid, char[][] nextgrid) {
        for (int numOfRow = 1; numOfRow <= numOfRow; numOfRow++) {
            for (int numOfCol = 1; numOfCol <= numOfCol; numOfCol++) {
                if (grid[numOfRow][numOfCol] == 'X') {
                    int count = 0;
                    {
                        if (grid[numOfRow][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow][numOfCol + 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow - 1][numOfCol + 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol - 1] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol] == 'X')
                            count = count + 1;
                        if (grid[numOfRow + 1][numOfCol + 1] == 'X')
                            count = count + 1;
                    }

                    if (count == 2 || count == 3) {
                        nextgrid[numOfRow][numOfCol] = 'X';
                    } else
                        nextgrid[numOfRow][numOfCol] = DOT;
                }
                if (grid[numOfRow][numOfCol] == DOT) {
                    int count1 = 0;
                    {
                        if (grid[numOfRow][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow - 1][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol - 1] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol] == 'X')
                            count1 = count1 + 1;
                        if (grid[numOfRow + 1][numOfCol + 1] == 'X')
                            count1 = count1 + 1;
                    }
                    if (count1 == 3)
                        nextgrid[numOfRow][numOfCol] = 'X';
                }
            }
        }
    }

    public static void copy(char[][] grid, char[][] nextgrid) {
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                grid[i][j] = nextgrid[i][j];
            }
        }
    }

    public static boolean isEmpty(char[][] grid, char[][] nextgrid) {
        boolean blankFlag = true;
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                if (grid[i][j] != DOT) {
                    blankFlag = false;
                }
            }
        }
        return blankFlag;
    }

    public static boolean isSame(char[][] grid, char[][] nextgrid) {
        boolean sameFlag = false;
        for (int i = 0; i < numOfRow + 1; i++) {
            for (int j = 0; j < numOfCol + 1; j++) {
                if (grid[i][j] == nextgrid[i][j]) {
                    sameFlag = true;
                    break;
                }
            }
        }
        return sameFlag;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);// erases panel Contents
        g.setColor(Color.black);
        if (sameFlag == false && blankFlag == false) {
            print(g);// or whatever method you use to print the world
        } else {
            if (sameFlag == true) {
                g.drawString("The worlds are repeating!", 10, 250);
            }
            if (blankFlag == true) {
                g.drawString("The world is blank!", 10, 250);
            }
        }
    }

}

问题的一部分是,您正在捕获一个常规异常,因此可能会出错。您应该打印异常中的堆栈跟踪,以帮助调试此问题,因为它将为您提供有关错误实际位置的更精确信息。例如,是在打开文件时还是在从打开的文件创建扫描程序时。Hopefu您正在了解为什么不应捕获一般异常


注意,这仅用于调试,因为打印堆栈跟踪是必要的。

文件必须位于目录项目中。 您将看到错误消息“替换您的源”:

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.txt");

此处缺少一些代码。请发布完整的代码。是否提供文件的完整路径?请分解代码并添加一些调试。不要将其分解为
world=new Scanner(new file(fileName));
,而是将其分解为
file file file=new file(fileName);
,然后是
System.out.println(file.getCanonicalPath());
,然后
world=新扫描仪(文件)
。这样它将打印出它要查找文件的路径。如果我的文件与Eclipse中的实际项目位于同一文件夹中,这有关系吗?我的意思是,我以前用文件读取器和写入器编写过程序,但从来没有遇到过这个问题。事实上,这应该更好,因为您需要做的只是specify文件名只需在任何地方打印出来,还是需要在try catch中执行?我不知道我做了什么,但添加了
ex.printStackTrace()
实际上让它读取了文件。现在我需要弄清楚如何让它进入下一代。我相信我所有的规则都编好了,但不会进入任何新一代。
   } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }