簡體   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