0

所以我有以下问题:编写一个程序,它接受一个整数命令行参数 N,并使用两个嵌套的 for 循环来打印一个 N×N 板,该板在 6 种颜色之间交替,由空格隔开。颜色用字母表示(如“r”代表红色,“b”代表蓝色)。你不能有两个相同颜色的相邻。

所以,我知道我可能需要数组来解决这个问题。我尝试了几种方法都出错了。以下是我最近的尝试之一,但我不确定现在如何通过网格并更正它。代码所做的是使每一行随机化,左右没有颜色相同,但列不固定。

请注意,我是一年级 CS 学生,没有编程历史。我猜这个问题的解决方案不是太复杂,但是,我看不到一个简单的解决方案......

    int N = StdIn.readInt();
    int array1[] = new int[N];
    for (int column = 0; column < N; column++) {
        int x = 0;

        for (int row = 0; row < N; row++) {

            int c = (int) (Math.random() * 6 + 1);

            while (x == c) {
                c = (int) (Math.random() * 6 + 1);
                array1[row] = c;
            }
            if (c == 1) {
                System.out.print("R ");
            }
            if (c == 2) {
                System.out.print("O ");
            }
            if (c == 3) {
                System.out.print("Y ");
            }
            if (c == 4) {
                System.out.print("G ");
            }
            if (c == 5) {
                System.out.print("B ");
            }
            if (c == 6) {
                System.out.print("I ");
            }

            x = c;

        }

        System.out.println();

    }
}
4

1 回答 1

1

这是我解决问题的方法。虽然很复杂,但背后的逻辑很简单。每次为二维数组分配新颜色时,只需检查要分配新颜色的位置的顶部和左侧的数组值。您只能在将颜色分配给数组的第一行之后才能执行此操作,但是您需要为第一行创建单独的条件。

public class ColourGrid {
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        char[][] clrGrid = new char[N][N];
        char colours[] = {'r','b','y','w','o','g'} ;
        for (int counter = 0 ; counter < N; counter++) {
            for (int counter2 = 0 ; counter2 < N; counter2++) {
                if (counter == 0 && counter2 == 0) {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                }
                else if (counter != 0 && counter2 == 0) {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    while (clrGrid[counter][counter2] == clrGrid[(counter)-1][counter2]) {
                        clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    }
                }
                else if (counter == 0 && counter2 != 0) {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    while (clrGrid[counter][counter2] == clrGrid[(counter)][counter2-1]) {
                        clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    }
                }
                else if (counter != 0 && counter2 != 0) {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    while (clrGrid[counter][counter2] == clrGrid[(counter)-1][counter2] || clrGrid[counter][counter2] == clrGrid[counter][(counter2)-1]) {
                        clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                    }
                }
                else {
                    clrGrid[counter][counter2] = colours[(int)(Math.random()* 5 + 1)] ;
                }
            }
        }
        for (int counter = 0 ; counter < N; counter++) {
            System.out.println("");
            for (int counter2 = 0 ; counter2 < N; counter2++) {
                System.out.print(clrGrid[counter][counter2] + " ");
            }
        }
    }
}
于 2014-03-26T11:30:52.040 回答