Reputation: 304
So I'm currently working on a personal project and I made a program that prints out star patterns. On one of the star patterns I want this output:
Figure
* *
** **
*** ***
**** ****
**********
So I made one method print out this:
Figure 1
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*" + " ");
}
System.out.println("");
}
*
**
***
****
*****
And another method print out this:
Figure 2
for (int i = 0; i < n; i++) {
for (int j = 2 * (n - i); j >= 0; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
*
**
***
****
*****
My question: How can I put the first method stars next to the other method stars?
This is what I got so far:
public static void printStarsVictory(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*" + " ");
}
System.out.println("");
}
for (int i = 0; i < n; i++) {
for (int j = 2 * (n - i); j >= 0; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
This is what is printing so far:
*
**
***
****
*****
*
**
***
****
*****
Any idea how to solve this?
Upvotes: 1
Views: 1091
Reputation:
Similar to this previous answer, you can use two nested for loops and one if else statement in the same way:
public static void main(String[] args) {
int n = 9;
for (int i = -n; i <= 0; i++) {
for (int j = -n; j <= n; j++)
if (Math.abs(i) <= Math.abs(j)
// in chessboard order
&& (i + j) % 2 == 0
// vertical borders
|| Math.abs(j) == n)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}
}
Output:
* *
** **
* * * *
** * * **
* * * * * *
** * * * * **
* * * * * * * *
** * * * * * * **
* * * * * * * * * *
** * * * * * * * **
See also:
• How to print a given diamond pattern in Java?
• Empty diamond shape with numbers
Upvotes: 0
Reputation:
You can visualize this figure as a matrix of numbers in a range: [-n, 0]
inclusive vertically and [-n, n]
inclusive horizontally, where each point is:
m[i][j] = Math.abs(i) - Math.abs(j);
If n = 4
, then this matrix looks like this:
0 1 2 3 4 3 2 1 0
-1 0 1 2 3 2 1 0 -1
-2 -1 0 1 2 1 0 -1 -2
-3 -2 -1 0 1 0 -1 -2 -3
-4 -3 -2 -1 0 -1 -2 -3 -4
int n = 4;
IntStream.rangeClosed(-n, 0)
.map(Math::abs)
.peek(i -> IntStream.rangeClosed(-n, n)
.map(Math::abs)
.mapToObj(j -> i > j ? " " : "* ")
.forEach(System.out::print))
.forEach(i -> System.out.println());
Output:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * *
See also: Making an hourglass using asterisks in java
Upvotes: 0
Reputation: 22370
I think you are on the right track you just need to combine your two programs into the inner for loop:
private static void printStarsVictory(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j < n * 2; j++) {
if (j <= i || j >= (n * 2 - i)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Example Usage printStarsVictory(5)
:
* *
** **
*** ***
**** ****
*********
Example Usage printStarsVictory(12)
:
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
***********************
Upvotes: 2
Reputation: 347
Well, System.out.println() prints only to the next row, not to the right. The only way is to create a new algorithm. You should put everything into single loop.
// loop starts from 0 and user counts from 1, so we wil decrement it by 1
n--;
// this loops moves pointer vertically
for (int i = 0; i < n; i++) {
// this loops moves pointer horisontally
for (int j = 0; j < n*2; j++) {
// here we choose what to print
if (j <= i || j >= n*2-i) {
System.out.print("*");
} else System.out.print(" ");
}
System.out.println();
}
Upvotes: 1
Reputation: 7302
Can you keep them in an array before printing to output:
public static void printStarsVictory(int n) {
StringBuilder[] lines = new StringBuilder[n + 1];
for (int i = 0; i < n; i++) {
lines[i] = new StringBuilder();
for (int j = 0; j <= i; j++) {
lines[i].append("*" + " ");
}
for (int j = 2 * (n - i - 1); j > 0; j--) {
lines[i].append(" ");
}
}
for (int i = 0; i < n; i++) {
for (int j = 2 * (n - i - 1); j > 0; j--) {
lines[i].append(" ");
}
for (int j = 0; j <= i; j++) {
lines[i].append("* ");
}
}
for (StringBuilder line : lines) {
System.out.println(line);
}
}
Upvotes: 1
Reputation: 3433
Each row has the increasing number of stars and decreasing number of spaces.
e.g. For n=5
first row has 2 stars at each side and 8 spaces.
At each iteration you can increase stars and decrease spaces and print them on the same line:
public static void printStarsVictory(int n) {
int sp = n * 2 - 2; // number of spaces
for (int i = 1; i <= n; i++) {
printStars(i); // print stars on the left side
int temp = sp;
while (temp > 0) {
System.out.print(" ");
temp--;
}
printStars(i); // // print stars on the right side
System.out.println("");
sp -= 2; // decrease spaces on each side
}
}
public static void printStars(int i) {
while(i>0) {
System.out.print("*");
i--;
}
}
Upvotes: 1