Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+19 votes

output

********8       
*******7
******6
*****5
****4
***3
**2
*1

input

public static void printNumbers(){
      for (int line = 1; line <= 8; line++){
          for (int star = 1; star <= (9 – line); star++){
               System.out.print(“*”);
                  }
        System.out.println(line);
        }
}

Does anyone know why in the output the solution says to use " System.out.println(line)" ?

asked in CSC211_Winter2018 by (1 point)
edited by
+5

I am not sure why this looks so strange when I post it but it is for the chapter 1 test review question 8.

I want to know why the answer suggest that you use System.out.println(line).....

I do not get how this would print out the desired number at the end of each line.

I would think you would want to use star instead of line here.

+4

I fixed the formatting of your post.

You probably want to mark those sections as "CODE" using the { } button when using the Q&A editor. Otherwise things like ** have special meaning (like bold or italics), because the Q&A website uses the "Markdown" markup language for formatting...

2 Answers

+9 votes

I think there's a problem with what you have written here, because the input you gave will not print what you have in the output. As is, your code will print the following to console:

********1
*******2
******3
*****4
****5
***6
**7
*8

This has to do with the variable line counting up from 1 when it should be counting down from 8, or perhaps a mathematical operation not being applied to the System.out.println(line); statement you have. Try switching things around so one matches the other and vice versa.

answered by (1 point)
–4 votes

It could be done without using a nested for loop!

answered by (-499 points)
...