繁体   English   中英

JAVA如何扫描.txt文件并使用charAt(int)将单个字符分配给2d布尔数组?

[英]JAVA How do you scan a .txt file and allocate individual characters it to a 2d boolean array using charAt(int)?

首先,感谢您抽出宝贵时间来解决问题。

我是一个完整的菜鸟(第3天编程),这说明了为什么我花了无数次尝试来解决此问题,但仍未弄清楚的原因:

问题:如何扫描由字符串(*和_)组成的.txt文件,并将其转换为布尔数组(即* = true和_ = false)(也如何打印出来?)猜测我们也需要double for循环)? 扫描程序似乎无法在主要方法中运行,并且出现“无此类文件”错误。

额外的问题:如何迭代网格(例如使用for循环),以使新网格替换旧网格,而最新网格变为不间断的新网格? 我对'show()'方法并不熟悉,但是我有点喜欢它。

这是我不完整的代码,可让您对问题有所了解:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class GameOfLife {

    public static boolean[][] gen() throws IOException {

        Scanner scanner = new Scanner(new File("seed.txt"));
        int rows = 0, columns = 0;
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            if (s.length() > columns)
                columns = s.length();
            rows++;
        }

        boolean[][] grid = new boolean[rows][columns + 1];
        Scanner scanner1 = new Scanner(new File("seed.txt"));
        String line;        
        for (int r = 0; r < rows; r++) {
            line = scanner1.nextLine();
            System.out.println(line);
            for (int c = 0; c <= line.length(); c++) {
                if (r == line.length()) {
                    break;
                }
                if(line.charAt(r) == '*') {
                grid[r][c] = true;
                }
            }
        }
        return grid;
    }

    public static boolean[][] nextGen(boolean[][] cells){
        boolean[][] newCells = new boolean[cells.length][cells[0].length];
        int num;
        for(int r = 0; r < cells.length; r++){
            for(int c = 0; c < cells[0].length; c++){
                num = numNeighbours(cells, r, c);
                if (occupiedNext(num, cells[r][c]))
                    newCells[r][c] = true;
            }
        }
        return newCells;
    }

    public static void main(String[] args) throws IOException{      
        boolean[][] cells = gen();
        show(cells);
        cells = nextGen(cells);
        show(cells);
        Scanner scanner2 = new Scanner(new File("seed.txt"));
        while(scanner2.nextLine().length() != 0){
            cells = nextGen(cells);
            show(cells);
        }
    }

    private static boolean inbounds(boolean[][] cells, int r, int c) {
        return r >= 0 && r < cells.length && c >= 0 &&
        c < cells[0].length;
    }

    private static int numNeighbours(boolean[][] cells, int row, int col) {
        int num = cells[row][col] ? -1 : 0;
        for (int r = row - 1; r <= row + 1; r++)
            for(int c = col - 1; c <= col + 1; c++)
                if (inbounds(cells, r, c) && cells[r][c] )
                    num++;
        return num;
    }

    public static void show(boolean[][] grid){
        String s = "";
        for(boolean[] row : grid){
            for(boolean val : row) 
                if(val)
                    s += "*";
                else
                    s += "_";
            s += "\n";
        }
        System.out.println(s);
    }

    public static boolean occupiedNext(int numNeighbours, boolean occupied){
        if (occupied && (numNeighbours == 2 || numNeighbours == 3))
            return true;
        else if (!occupied && numNeighbours == 3)
            return true;
        else
            return false;
    } 
}

我只想提出一个可能对您有所帮助的解决方案。 尝试读取文件内容并将其分配给字符串变量。 现在使用string.replaceAll方法将(* and _)* = true and _ = false 比将更新的字符串写入文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM