Java棋盘,奇/偶 %2 板

3
我已经成功地使用彩色面板将棋盘拼接在一起,但只有当用户输入奇数的行和列时才能实现。否则,输入偶数时,它只显示交替着颜色的列。我正在努力编写一个短代码段,通过使用%2=0来检查是否为奇数或偶数,以及在结果为偶数时改变颜色。以下是我的代码。谢谢,并请对我好一点,我很新学编程! :-)
此外,我创建了一个单独的ColorPanel类来构建彩色面板,然后将其引入我的主要程序中。我没有在下面放置那些代码。
import javax.swing.*;
import java.awt.*;

public class Checkerboard extends JPanel{

public static void main(String[] args) {
    JFrame chBoard = new JFrame();
    chBoard.setTitle("Checkerboard");
    chBoard.setSize(800,800);
    chBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String inputStr = JOptionPane.showInputDialog("Number of rows", "5");
    if (inputStr == null) return;
    int row = Integer.parseInt(inputStr);

    inputStr = JOptionPane.showInputDialog("Number of columns", "5");
    if (inputStr == null) return;
    int col = Integer.parseInt(inputStr);

    Container pane = chBoard.getContentPane();
    pane.setLayout(new GridLayout(row, col));

    Color BoxColor = Color.red;

    for ( int counter = 1;  counter <= row * col;  counter++ )
      {
        if (BoxColor == Color.red)
             BoxColor = Color.black;
        else
             BoxColor = Color.red;

        ColorPanel panel = new ColorPanel(BoxColor);
        pane.add(panel);
      }

    chBoard.setVisible(true);
}
}
2个回答

1
将您的循环更改为:

for ( int x = 0;  x < row;  x++ ) {
    for(int y = 0; y < col; y++) {
        if((x + y)%2 == 0) {
            BoxColor = Color.red;
        } else {
            BoxColor = Color.black;
        }

        ...

    }
}

非常感谢,这很有道理。 - izzyfanto

0

就像我说的一样,我是编程新手,但我非常享受学习的过程。我希望这能帮助其他人在他们的学习经历中有所收获。

无论如何,我想我为自己创造了更多的工作,因为我创建了一个单独的ColorPanel类。所以,我改变了它,使用现有的JPanel类在主程序中创建面板来构建彩色面板。所以,不再是:

ColorPanel panel = new ColorPanel(BoxColor); 

+ ColorPanel类...

我放置:

JPanel panel = new JPanel();
panel.setBackground(BoxColor);

在主程序中删除了额外的ColorPanel类。

抱歉有些重复,只是想要清楚地解释一下自己。

此外,感谢Jason,他真的帮助我理解了使用这两个的想法

int x & y

计算

row & col

然后将它们相加,这使我能够使用

%2=0

为了确定我在奇数面板还是偶数面板上。

希望这能帮助到某些人!:-)

最终代码如下:

import javax.swing.*;
import java.awt.*;

public class Checkerboard extends JPanel{

public static void main(String[] args) {
    JFrame chBoard = new JFrame();
    chBoard.setTitle("Checkerboard");
    chBoard.setSize(800,800);
    chBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String inputStr = JOptionPane.showInputDialog("Number of rows", "5");
    if (inputStr == null) return;
    int row = Integer.parseInt(inputStr);

    inputStr = JOptionPane.showInputDialog("Number of columns", "5");
    if (inputStr == null) return;
    int col = Integer.parseInt(inputStr);

    Container pane = chBoard.getContentPane();
    pane.setLayout(new GridLayout(row, col));

    Color BoxColor = Color.red;

    for ( int x = 0;  x < row;  x++ ) {
        for(int y = 0; y < col; y++) {
            if((x + y)%2 == 0) {
                BoxColor = Color.red;}
            else{
             BoxColor = Color.black;}

        JPanel panel = new JPanel();
        panel.setBackground(BoxColor);

        pane.add(panel);
      }

    chBoard.setVisible(true);
}
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接