I am very close to this, however instead of the asterisks being horizontal, in a row, they are vertical, one in top of the other.
Question: Write a program that displays the output below. Use a variable to store the value for the number of asterisks in the first row. You must use multiple loops in your solution.
**********
*********
********
*******
******
*****
****
***
**
*
I currently have:
public class JavaAssignment6b {
public static void main( String[] args ) {
for(int r = 1; r <= 10; r++) {
for(int j = 10; j >= r; j--) {
System.out.println("*");
}
System.out.println(" ");
}
}
}
Simple answer to this question. All you have to do is add a simple if statement. I have posted two solutions below, one prints the number of asterisks on the first line, the other posts the number of asterisks on every line.
To print the number of asterisks on the first line:
int numberOfAsterisks = 0;
for(int r = 1; r <= 10; r++) {
for(int j = 10; j >= r; j--) {
System.out.print("*");
numberOfAsterisks++;
}
if (r==1) {
System.out.print(numberOfAsterisks);
}
System.out.println();
}
}
And the output
**********10
*********
********
*******
******
*****
****
***
**
*
To print the number of asterisks on every line:
int numberOfAsterisks = 0;
for(int r = 1; r <= 10; r++) {
for(int j = 10; j >= r; j--) {
System.out.print("*");
numberOfAsterisks++;
}
System.out.print(numberOfAsterisks);
numberOfAsterisks = 0;
System.out.println();
}
}
and the output for this answer:
**********10
*********9
********8
*******7
******6
*****5
****4
***3
**2
*1