gpt4 book ai didi

java - 使用双数组绘制三角形

转载 作者:行者123 更新时间:2023-11-29 04:49:28 25 4
gpt4 key购买 nike

所以我需要帮助使用双数组/矩阵来绘制这样的三角形:

     *     *     *
*** *** ***
***** ***** *****
*******************

使用矩阵和双数组。

public static void main(String[] args) {

char[][] d = new char[4][7];

for(int i = 0;i<4;i++){

for(int j=0;j<7;j++){
d[i][j] = ' ';
}
}

///


for(int b = 0;b<4;b++){

for(int g=0;g<7;g++){

System.out.print(d[b][g]);
}
System.out.println();


}
}

当你运行它时,你什么也得不到,但那是因为d[i][j] = ' ';

请帮忙?

最佳答案

你想画一个三角形,那就画吧。

for(int i = 0;i<4;i++){
for(int j=0;j<7;j++){
d[i][j] = (Math.abs(7/2-j)<=i ? '*' : ' ');
}
}

关于java - 使用双数组绘制三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35967265/

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