如何使用 Java for 循环生成这样的输出?

问题描述 投票:0回答:1
1      2      3    
4      5      6   
7      8      9

我正在用这个制作井字游戏;-;

我尝试了一个公式,但结果只是

0    0    0
1    1    1
2    2    2

如果您能指导我如何在选择时将数字更改为 X/O,我也将不胜感激。

int[][]x = new int[3][3]

for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) {

System.out.print(" "); 系统。出去。打印(i+1+j+“”);

}

系统。出去。打印() }

抱歉,我对此真的很陌生TT

java for-loop tic-tac-toe
1个回答
0
投票
public class PatternPrinter {
    public static void main(String[] args) {
        int rows = 3;
        int cols = 3;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                System.out.print((i - 1) * cols + j + "\t");
            }
            System.out.println();
        }
    }
}

输出:

1      2      3    
4      5      6   
7      8      9
© www.soinside.com 2019 - 2024. All rights reserved.