Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Images courtesy of wolframalpha.com

###4-abs(x)

4-abs(x)

enter image description here

###4-abs(3 - x)

4-abs(3 - x)

enter image description here

Finally, here is a very flexible solution, which also works with even numbers:

int rows = 20;
double maximumValue = Math.ceil(rows / 2.0);
double shifted = maximumValue - 1;
for (int i = 0; i < rows; i++) {
 int count = (int) (maximumValue - Math.abs(shifted - i));
 if (i >= rows / 2 && rows % 2 == 0) // slight fix for even number of rows
 count++;
 
 for (int numStars = 0; numStars < count; numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This will output:

*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Images courtesy of wolframalpha.com

###4-abs(x)

enter image description here

###4-abs(3 - x)

enter image description here

Finally, here is a very flexible solution, which also works with even numbers:

int rows = 20;
double maximumValue = Math.ceil(rows / 2.0);
double shifted = maximumValue - 1;
for (int i = 0; i < rows; i++) {
 int count = (int) (maximumValue - Math.abs(shifted - i));
 if (i >= rows / 2 && rows % 2 == 0) // slight fix for even number of rows
 count++;
 
 for (int numStars = 0; numStars < count; numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This will output:

*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Images courtesy of wolframalpha.com

4-abs(x)

enter image description here

4-abs(3 - x)

enter image description here

Finally, here is a very flexible solution, which also works with even numbers:

int rows = 20;
double maximumValue = Math.ceil(rows / 2.0);
double shifted = maximumValue - 1;
for (int i = 0; i < rows; i++) {
 int count = (int) (maximumValue - Math.abs(shifted - i));
 if (i >= rows / 2 && rows % 2 == 0) // slight fix for even number of rows
 count++;
 
 for (int numStars = 0; numStars < count; numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This will output:

*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
added 790 characters in body
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Images courtesy of wolframalpha.com

###4-abs(x)

enter image description here

###4-abs(3 - x)

enter image description here

Finally, here is a very flexible solution, which also works with even numbers:

int rows = 20;
double maximumValue = Math.ceil(rows / 2.0);
double shifted = maximumValue - 1;
for (int i = 0; i < rows; i++) {
 int count = (int) (maximumValue - Math.abs(shifted - i));
 if (i >= rows / 2 && rows % 2 == 0) // slight fix for even number of rows
 count++;
 
 for (int numStars = 0; numStars < count; numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This will output:

*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Images courtesy of wolframalpha.com

###4-abs(x)

enter image description here

###4-abs(3 - x)

enter image description here

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Images courtesy of wolframalpha.com

###4-abs(x)

enter image description here

###4-abs(3 - x)

enter image description here

Finally, here is a very flexible solution, which also works with even numbers:

int rows = 20;
double maximumValue = Math.ceil(rows / 2.0);
double shifted = maximumValue - 1;
for (int i = 0; i < rows; i++) {
 int count = (int) (maximumValue - Math.abs(shifted - i));
 if (i >= rows / 2 && rows % 2 == 0) // slight fix for even number of rows
 count++;
 
 for (int numStars = 0; numStars < count; numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This will output:

*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
added 272 characters in body
Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Images courtesy of wolframalpha.com

###4-abs(x)

enter image description here

###4-abs(3 - x)

enter image description here

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Here is a lovely way to do it using only one nested for-loop:

for (int i = 0; i < 7; i++) {
 for (int numStars = 0; numStars < 4 - Math.abs(3 - i); numStars++) {
 System.out.print("*");
 }
 System.out.println();
}

This uses the Math.abs function to perform the calculation of how many stars to print.

If we take a look at a plot of the classic Math.abs we can see that it looks useful. We need to flip it upside-down though, this is done by taking 4 - abs(x) which would look like this. Finally, we need to switch it to the right a bit, so we modify the input to the function call and end up with this: 4 - abs(3 - x)

Images courtesy of wolframalpha.com

###4-abs(x)

enter image description here

###4-abs(3 - x)

enter image description here

Source Link
Simon Forsberg
  • 59.7k
  • 9
  • 157
  • 311
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /