• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

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

0
flo_the_lil_minx
Asked:
flo_the_lil_minx
  • 5
  • 2
  • 2
2 Solutions
 
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
 
Kevin CrossChief 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
 
contactkarthiCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now