For和while循环来获得这种装饰 [英] For and while loop to get this decoration
问题描述
我得到了这个结果:
-
-
-
-
*
*
*
*
*
但我希望看到:
---- *
- - **
- ***
- ****
*****
我尝试过:
包演示;
公共类酯类{
public static void main(String [] args){
// ---- * ////// x = - / 4 /
// --- ** ///// y = * / 5 /
// - ***
// - ****
// *****
for(int i = 1; i< 5;){
System.out.println( - );
i ++;
}
for(int y = 5; y> = 1;){
System.out.println('*');
y - ;
}
}
}
I got this result:
-
-
-
-
*
*
*
*
*
but I want to see:
----*
---**
--***
-****
*****
What I have tried:
package demo;
public class esterics {
public static void main(String[] args) {
//----* ////// x = - /4/
//---** ///// y = * /5/
//--***
//-****
//*****
for (int i = 1;i<5;){
System.out.println("-");
i++;
}
for (int y = 5; y>=1;){
System.out.println('*');
y --;
}
}
}
推荐答案
首先不使用println
一直以来。每次调用它时,它会在输出上创建一个新行,您只需要在每行之后,而不是每个字符。
使用System.out.print
打印行中的每个字符,然后println
完成准备下一行的行。
并且......你将不得不嵌套这些循环
Start by not usingprintln
all the time. Each time you call it, it creates a new line on the output, and you only need that after each row, not each character.
UseSystem.out.print
to print each individual character in the row, thenprintln
to finish the row ready for the next one.
And ... you are going to have to "nest" those loops
for (int y = ... // for each row
for (int x = ... // for each character in the row
// do the character
}
// use println to end the row ready for the next one
...
}
这篇关于For和while循环来获得这种装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!