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

選択肢は 2 つあります。(a) 配列を反復処理して値を自分で出力するか、(b) Arrays.toString()println の前に呼び出してから使用します。replace(",", "")

于 2013-09-16T11:52:08.190 に答える
0

String.replace(",", "");コンマの出現箇所を置換します。したがって、その呼び出しの前Arrays.toString()に、配列の文字列表現を取得してからreplace()メソッドを呼び出します。

于 2013-09-16T11:53:37.617 に答える