0

您好,到目前为止,我已经编写了这段代码,它工作正常,但是输出不符合我的期望,我希望有一组干净的 * 和空格,它给了我这样的东西,,,,*,* 等我将如何工作围绕这个

import java.util.*;

class Cell4
{
    public static void main (String[] args)
    {
        int l; //length of a row
        int r; //stores the rule sequence
        int g; //number of the generation
        String a;//stands for automata
        int p; //position of the true cells
        int currentG; //current generation

        Scanner scanner;
        scanner= new Scanner(System.in);
        a=scanner.next();
        l=scanner.nextInt();
        g=scanner.nextInt();
        scanner.next();
        boolean[][] cellsCurrent = new boolean[g][l+2]; 
        currentG=0;
        while(scanner.hasNextInt()){ //put the position values in an array
            p=scanner.nextInt();
            if(p<=l){
                cellsCurrent[currentG][p] = true;
            }
        }
        scanner.next();

        if(a.equals("A")){
            for(currentG=0; currentG<g-1; currentG++){ //for all generations
                for(int i=1; i<l+1; i++){ //for all cells
                    if(cellsCurrent[currentG][i] == true && (cellsCurrent[currentG][i+1] == true ^ cellsCurrent[currentG][i-1] == true)){ 
                        cellsCurrent[currentG+1][i] = true;
                    }
                    else if (cellsCurrent[currentG][i] == true){
                        cellsCurrent[currentG+1][i] = false;
                    }

                    if(cellsCurrent[currentG][i] == false && cellsCurrent[currentG][i+1] == false && cellsCurrent[currentG][i-1] == false){
                        cellsCurrent[currentG+1][i] = false;
                    }
                    else if(cellsCurrent[currentG][i] == false){
                        cellsCurrent[currentG+1][i] = true; 
                    }
                }
            }
        }

        if(a.equals("B")){
            for(currentG=0; currentG<g-1; currentG++){ //for all generations
                for(int i=1; i<l+1; i++){ //for all cells
                    if(cellsCurrent[currentG][i] == true && cellsCurrent[currentG][i+1] == false){ 
                        cellsCurrent[currentG+1][i] = true;
                    }
                    else if (cellsCurrent[currentG][i] == true){
                        cellsCurrent[currentG+1][i] = false;
                    }

                    if(cellsCurrent[currentG][i] == false && (cellsCurrent[currentG][i+1] == true ^ cellsCurrent[currentG][i-1] == true)){ 
                        cellsCurrent[currentG+1][i] = true;
                    }
                    else if (cellsCurrent[currentG][i] == false){
                        cellsCurrent[currentG+1][i] = false;
                    }
                }
            }
        }

        if (a.equals("U")){
            boolean[] rules = new boolean[8];

            for(int i=0;i<8;i++){ //put the position values in an array
                r=scanner.nextInt();
                if(r == 1){
                    rules[i] = true;
                }

                else if(r == 0){
                    rules[i] = false; 
                }
            }

            for(currentG=0; currentG<g-1; currentG++){ //for all generations
                for(int i=1; i<l+1; i++){ //for all cells
                    if(cellsCurrent[currentG][i] == false && cellsCurrent[currentG][i+1] == false && cellsCurrent[currentG][i-1] == false){
                        cellsCurrent[currentG+1][i] = rules[0]; 
                    }
                    else if(cellsCurrent[currentG][i] == false && cellsCurrent[currentG][i-1] == false && cellsCurrent[currentG][i+1] == true){
                        cellsCurrent[currentG+1][i] = rules[1]; 
                    }
                    else if(cellsCurrent[currentG][i] == true && cellsCurrent[currentG][i-1] == false && cellsCurrent[currentG][i+1] == false){
                        cellsCurrent[currentG+1][i] = rules[2]; 
                    }
                    else if(cellsCurrent[currentG][i] == true && cellsCurrent[currentG][i-1] == false && cellsCurrent[currentG][i+1] == true){
                        cellsCurrent[currentG+1][i] = rules[3] ;
                    }
                    else if(cellsCurrent[currentG][i] == false && cellsCurrent[currentG][i-1] == true && cellsCurrent[currentG][i+1] == false){
                        cellsCurrent[currentG+1][i] = rules[4] ;
                    }
                    else if(cellsCurrent[currentG][i] == false && cellsCurrent[currentG][i-1] == true && cellsCurrent[currentG][i+1] == true){
                        cellsCurrent[currentG+1][i] = rules[5] ;
                    }
                    else if(cellsCurrent[currentG][i] == true && cellsCurrent[currentG][i-1] == true && cellsCurrent[currentG][i+1] == false){
                        cellsCurrent[currentG+1][i] = rules[6] ;
                    }
                    else if(cellsCurrent[currentG][i] == true && cellsCurrent[currentG][i-1] == true && cellsCurrent[currentG][i+1] == true){
                        cellsCurrent[currentG+1][i] = rules[7] ;
                    }
                }
            }
        }

        for (int i = 0; i < cellsCurrent.length; i++) {

            System.out.println(Arrays.toString(cellsCurrent[i]).replace("true","*").replace("false"," "));

        }
    }
}
4

2 回答 2

0

你有两个选择。要么(a)遍历数组并自己打印值,要么(b)Arrays.toString()在 println 之前调用,然后使用replace(",", "")

于 2013-09-16T11:52:08.190 回答
0

使用String.replace(",", "");替换出现的逗号。所以在调用之前Arrays.toString()检索数组的字符串表示,然后调用replace()方法。

于 2013-09-16T11:53:37.617 回答