What am I doing wrong?

Hi there,

My task is to create a program using nested loops to create a display of stars on the screen that look like this...

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

I can't see what is wrong with my code, there's probably something really silly missing.  Please could someone point me in the right direction?

TIA Flo


//Backward Triangle
 
import java.util.*;
public class BackwardTriangle
{
        public static void main (String[] args);
        
        //outer loop to count rows
        
        for (int row = 1; row <=5; row++)
      {  
        //first inner loop to count spaces
        
        for (int space = 0; space < row -1; space--)
        
        {
          System.out.print(" ");
         }
         
         //second inner loop to count stars
         
         for (int star =1; star <=5 -row; star++)
         
         {
         
          System.out.print("*");
          
          }
          System.out.println();
          
    }
}

Open in new window

LVL 1
flo_the_lil_minxAsked:
Who is Participating?
 
Kevin CrossConnect With a Mentor Chief Technology OfficerCommented:
As far as the logic goes -- here are some hints/direction points:

// this says start at 0 and loop until 0 < 0 (row == 1 on first attempt)
// this goes no where OR worst could end up in an infinite loop as you are subtracting 1 from 0 each time.
for (int space = 0; space < row -1; space--)

// starting at row == 1 you will get 5 -1 stars
// check logic on number of stars you want to display by row
for (int star =1; star <=5 -row; star++)

Hopefully that helps.

Give it a try and post back your attempt and will answer questions if you have any.
0
 
Kevin CrossChief Technology OfficerCommented:
Firstly, you are missing the opening { and closing } for your main method.  You have a semi-colon (;) instead -- so you need to remove that.

public static void main(String[] args) {
    // ... rest of code ...
}
0
 
contactkarthiConnect With a Mentor Commented:
for (int space = 0; space < row -1; space--)

the above code will lead you to an infinate loop...

should be space++

the problem with your code is with the loops you use to print stars and spaces..

i just changed it as below

let me know if you have any questions
import java.util.*;
public class BackwardTriangle
{
	public static void main (String[] args)
	{
 
		// outer loop to count rows
 
		for (int row = 1; row <=5; row++)
		{  
			// first inner loop to count spaces
 
			for (int space = 0; space < 5 -row; space++)
 
			{
				System.out.print(" ");
			}
 
			// second inner loop to count stars
 
			for (int star =1; star <=row; star++)
 
			{
 
				System.out.print("*");
 
			}
			System.out.println();
 
		}
	}
}

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Kevin CrossChief Technology OfficerCommented:
contactkarthi,

I intentionally didn't post the full source solution as the phrasing of the question suggested this was homework.

Anyway, since you have already posted code, your statement that space-- won't work is not entirely correct.  I had this coded when I first responded and works just fine.
for (int space = 5 - row; space > 0; space--)

Open in new window

0
 
flo_the_lil_minxAuthor Commented:
Thank you both for your answers.  It is true that it is a homework task so mwvisa1 did the right thing in not putting the code up initialy.  But, to be honest, I had grasped the concept of loops etc (you start with a value, you test it against a condition, if it meets the condition continue the loop, if not exit the loop) but after the day I've had I was grateful for the code.  It made it easier for me to compare it with mine so I could see where I was going wrong so both approaches were spot on!  Thanks again and thanks for the speedy replies
0
 
contactkarthiCommented:
mwvisa1 i normally dont post the complete code when somebody asks can some one give me a solution for this problem. he almost had this thing written except for the 2 for statements. for the first statement i posted the clarification and for the second it is just the value part that had to be changed so i tweeked it a bit.. instead of just putting the 2 lines i just put the block so that the asker could see the difference.. sorry if  i am wrong..
0
 
Kevin CrossChief Technology OfficerCommented:
No need to be sorry.  I was just passing along the guidance others have given me.  You are fine.  We are all just helping.  I am no pro at this...still learning the EE way myself.

:)

0
 
flo_the_lil_minxAuthor Commented:
Group hug time :)

BTW Flo = she
0
 
Kevin CrossChief Technology OfficerCommented:
LOL, that makes perfect sense -- made the mistake of looking at the avatar being male. :) Forgot that was the default.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.