gpt4 book ai didi

java - 如何使方形星形图案看起来像棋盘?

转载 作者:行者123 更新时间:2023-11-30 08:10:33 25 4
gpt4 key购买 nike

我有 4 行和 5 列,我希望其中的几颗星星消失,使其像棋盘一样:

在第一行,第二和第四颗星消失。第二行,第一行,第三行和第五行消失。第三排,第二颗和第四颗星消失。第四排,第一、第三和第五消失。

package starpattern;

public class square {

public static void main(String[] args) {
for(int i=1; i<=4; i++)
{
for(int j=1;j<=5;j++)
{
System.out.print("*");
}
System.out.println();
}

}

最佳答案

final int numRows = 4;
final int numCols = 5;

for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numCols; j++)
{
//the places where '*' is printed is where the row and col add to an even number
String s = (((i + j) & 1) == 0) ? "*" : " ";
System.out.print(s);
}
System.out.println();
}

关于java - 如何使方形星形图案看起来像棋盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31639132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com