What about changing <= 7
to <= 9
(or most likely a variable holding the input number)
Change :
for (int i = 1; i <= 7; i++)
to
for (int i = 1; i <= lines; i++)
Вопрос
I am getting the hang of using nested for loops to make numeric patterns. This code works just fine as long as I do not enter a number above 7. for example, when I put in 7 it looks like this (no spaces):
1
212
32123
4321234
32123
212
1
But when I enter 9 it looks like this:
1
212
32123
4321234
543212345
4321234
32123
What am I doing wrong to make it not finish the diamond?
public void displayPatternVI (int lines)
{
System.out.println("\n\tMy Own Pattern to be implemented\n");
int columns = 1;
int start = 0;
int spaces = lines / 2;
for (int i = 1; i <= 7; i++)
{
for (int j = 1; j <= spaces; j++)
{
System.out.print (" ");
}
if(i < lines/2+1)
{
start = i;
spaces = spaces - 1;
}
else
{
start = (lines + 1 ) - i;
spaces = spaces + 1;
}
for (int j = 1; j <= columns; j++)
{
int midColumn = columns / 2 + 1;
System.out.print(start);
if (j < midColumn)
{
start--;
}
else
{
start++;
}
}
System.out.println();
if(i<lines/2+1)
{
columns = columns + 2;
}
else
{
columns = columns - 2;
}
}
System.out.println();
}
Решение
What about changing <= 7
to <= 9
(or most likely a variable holding the input number)
Change :
for (int i = 1; i <= 7; i++)
to
for (int i = 1; i <= lines; i++)