[英]Making pattern using loops Java
I'm supposed to create some pattern - somewhat triangular - using for loop based on given number n. 我应该创建一些模式-有点三角形-使用基于给定数字n的for循环。
For example, if given number n is 3, the pattern should be something like this : 例如,如果给定的数字n为3,则模式应如下所示:
**
*##*
*####*
And below is the piece of code I'm currently working on now. 下面是我目前正在处理的代码。
public static void patterPrinters(int n) {
for (int k = 0; k < n; k++) {
for ( int x = n; x > k + 1; x--) {
System.out.print(" ");
}
for ( int z = n - k; z <= n; z++) {
System.out.print("**");
}
System.out.print("\n");
}
}
}
So far, I was able to make a similar shape, but of course, it is filled with stars (*) without the number signs(#) in between them. 到目前为止,我能够做出类似的形状,但是当然,它充满了星星(*),而星星和星星之间没有数字符号(#)。 Like :
喜欢 :
**
****
******
Could someone give me a hint as what I'm supposed to do from here? 有人可以给我提示我应该在这里做什么吗?
public static void patterPrinters(int n) {
int i,j,k;
for( i=0;i<n;i++)
{
for(k=0;k<((n-1)-i);k++)
{
System.out.print(" ");
}
System.out.print("*");
for(j=0;j<(i*2);j++)
{
System.out.print("#")
}
System.out.print("*\n");
}
}
check this out. 看一下这个。 All the Best.
祝一切顺利。
HINT: 暗示:
If you managed to print the correct shape, but with *
only, you can easily fix this by printing a single *
at the start and end of each row, and between them print x-2 #
s, where x is the number of *
you currently print in each row. 如果您设法打印正确的形状,但仅使用
*
,则可以通过在每行的开头和结尾处打印单个*
并在它们之间打印x-2 #
s来轻松解决此问题,其中x是*
的数量您当前在每一行中打印。
public static void patterPrinters(int n) 公共静态无效patterPrinters(int n)
{
for (int k = 0; k < n; k++)
{
for (int x = n; x > k + 1; x--)
{
System.out.print(" ");
}
System.out.print("*");
for (int col = 0; col < k; col++)
{
System.out.print("##");
}
System.out.print("*\n");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.