我怎样才能打印一个三角形与“*”在Java循环使用? [英] How can i print a triangle with "*"s using for loop in java?
问题描述
*
**
***
****
*****
******
*******
********
*********
**********
<等等。任何人都可以帮我吗?
public class Project1 {
public static void main(String [] args){
int c, d,e; (c = 1; c <= 8; c ++){
for(d = 1; d <= c; d ++){
System.out.print(* );
}
System.out.println(); (e = 1; e <= 4; e ++){
}
System.out.println(***);
这是我发现的从互联网,但我不明白为什么它使用两个循环。 (我明白用来构造干的)。 (String [] args)
{
StringBuilder stars = new StringBuilder();
(int i = 0; i <= 10; i ++)
{
stars.append(*);
System.out.println(stars);
或者使用嵌套循环:(这是练习是真的试图让你做的)
public static void main( (int i = 0; i <= 10; i ++)
{
for(int j = 0; j< = i; j ++)
{
System.out.print(*);
}
System.out.print(\\\
);
}
}
I want to draw a triangle with stars like below using for loop, but i really don't have any idea of how to do this ? Triangle is going to be like this:
*
**
***
****
*****
******
*******
********
*********
**********
and so on. Can anybody please help me ?
public class Project1 {
public static void main (String[] args){
int c, d, e;
for (c = 1 ; c <= 8 ; c++){
for (d = 1 ; d <= c ; d++){
System.out.print ("*");
}
System.out.println("");
}
for (e = 1 ; e <= 4 ; e++){
System.out.println ("***");
}
}
}
This is what i have found from the internet, but i did not understand the reason why it uses two loops. ( i understood the one used to construct the stem. )
解决方案 public static void main(String[] args)
{
StringBuilder stars = new StringBuilder();
for(int i = 0; i <= 10; i++)
{
stars.append("*");
System.out.println(stars);
}
}
Or alternatively using nested loops: (This is what the exercise was really trying to get you to do)
public static void main(String[] args)
{
for(int i = 0; i <= 10; i++)
{
for(int j=0; j<=i; j++)
{
System.out.print("*");
}
System.out.print("\n");
}
}
这篇关于我怎样才能打印一个三角形与“*”在Java循环使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!