我该如何使用循环呢? [英] how can i do this using loops ?
本文介绍了我该如何使用循环呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做这样的形状:
* * * * *
* * * *
* * *
* *
*
代码:
public void displayPyramid(int x, int y) { row = x; col = y; int r =0; int c =0; while(r<row)> { while(c<col)> { System.out.println("* "); c++; } System.out.println(); r++; } }
解决方案
算法步骤:
1.从5步到1步-1开始循环
2.根据当前循环号显示星星
3.循环减小1
时,请换行
完毕!尝试!
查看图表并计算开头的空格数以及每行上的星星总数.这样应该可以为您提供关于每行所需的循环计数器的合理线索.for (int i = 5; i > 0; i--) { int y = 5; y = y - i; for (int e = y; e > 0; e--) { Console.Write(" "); } for (int j = i; j > 0; j--) { Console.Write("* "); } Console.Write("\n"); }
Eshaq
i want to do this shape :
* * * * *
* * * *
* * *
* *
*
code :
public void displayPyramid(int x, int y) { row = x; col = y; int r =0; int c =0; while(r<row)> { while(c<col)> { System.out.println("* "); c++; } System.out.println(); r++; } }
解决方案
Algo Steps:
1. Start loop from 5 to 1 step -1
2. Show the stars as per the current loop number
3. Put a line break when the loop is reduced by 1
Done! Try!
Look at your diagram and count the number of spaces at the beginning, and total number of stars on each line. That should give you a reasonable clue as to the loop counters you need for each row.
You can try this, may it help you
for (int i = 5; i > 0; i--) { int y = 5; y = y - i; for (int e = y; e > 0; e--) { Console.Write(" "); } for (int j = i; j > 0; j--) { Console.Write("* "); } Console.Write("\n"); }
Eshaq
这篇关于我该如何使用循环呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文