英文:
I´m trying to print a misaligned pyramid
问题
Sure, here's the translated code as you requested:
我在尝试打印一个带有不对齐间距的金字塔,就像这样:
*
**
***
****
*****
******
我目前正在使用以下代码,它给我一个完全对齐的金字塔,但我期望的结果是上面我指定的间距:
//Chueca
for (int i = 1, j = 0; i <= numLineas; ++i, j = 0) {
for (int espacio = 1; espacio <= numLineas - i; ++espacio) {
System.out.print(" ");
}
while (j != i) {
System.out.print("*");
++j;
}
System.out.println();
}`
有没有办法修改这段代码以实现这一点?
英文:
I´m having a little bit of trouble printing a pyramid with misaligned spacing, kinda like this:
*
**
***
****
*****
******
I´m currently using this code which gives me a perfectly aligned pyramid, but my desired result is the spacing I specified above
//Chueca
for (int i = 1, j = 0; i <= numLineas; ++i,j=0){
for(int espacio = 1; espacio <= numLineas-i; ++espacio){
System.out.print(" ");
}
while(j != i){
System.out.print("*");
++j;
}
System.out.println();
}`
IS there any way to modify this code to achieve this?
答案1
得分: 0
更改第二个“for”循环的条件,只打印一半的空格:
```lang-java
for (int espacio = 1; espacio <= (numLineas - i) / 2; ++espacio) {
输出
*
**
***
****
*****
******
英文:
Change the condition of the second for
loop to only print half the spaces:
for (int espacio = 1; espacio <= (numLineas - i) / 2; ++espacio) {
Output
*
**
***
****
*****
******
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论