Welcome to Dream.In.Code
Become a Java Expert!

Join 150,413 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,015 people online right now. Registration is fast and FREE... Join Now!




Using nested for loops to print shapes.

 
Reply to this topicStart new topic

Using nested for loops to print shapes.

mattpd
15 Mar, 2008 - 06:47 PM
Post #1

New D.I.C Head
*

Joined: 15 Mar, 2008
Posts: 13

I have two assignments due soon and I need assistance with one. The first is to use for loops to print a design like this:

*
**
***
****
*****
and so on... My code for this is as follows:

CODE

public class tree {
        
  
    public tree() {
    }
    
  
    public static void main(String[] args) {
        int line;
        int star;
        int row=1;
        
        for (line=0; line<10; line++) {
            
            for (star=0; star<row; star++)
                System.out.print ('*');
                        System.out.println ();
                        row++;
        }
        
    
    
    }  
        
}


The next assignment is the one that is troubling me. I need to print the classic "Christmas tree" design, an Isosceles triangle. I cant seem to preserve the white space on this forum so I can't draw it out but you get the picture.


Any hints on how to modify my original code to print this design? I am guessing I need to print blank spaces before the *, and decrement the spaces while incrementing the * each line. Just can't wrap my head around how to do it.
User is offlineProfile CardPM
+Quote Post

capty99
RE: Using Nested For Loops To Print Shapes.
15 Mar, 2008 - 07:12 PM
Post #2

the real kya
Group Icon

Joined: 26 Apr, 2001
Posts: 9,260



Thanked: 16 times
Dream Kudos: 550
My Contributions
i hashed one out and its 3 for loops, one to do each row, and then one to print the spaces, and one to print the astericks. the spaces and astericks are in line as opposed to nested.

so in pseudocode
for (rows)
for(spaces){
}


wow. that got cut off and i don't have an edit button.


anyways, this is quick and dirty.
it also makes a very ugly tree, however should point you in the right direction.

what i did in the past is we took an input from the user for the size of the then modified our output depending on if their input was odd or even.

java

public class tree {


public tree() {
}


public static void main(String[] args) {
int input = 10;
double spaces = input/2;
for(int i=0; i < input; i++) {
for(int s = 0; s < spaces; s++){
System.out.print(" ");
}
for(int k = 0; k < i; k++){
System.out.print("*");
}
System.out.print("\n");
spaces= spaces-.5;
}
}
}

User is offlineProfile CardPM
+Quote Post

mattpd
RE: Using Nested For Loops To Print Shapes.
15 Mar, 2008 - 07:24 PM
Post #3

New D.I.C Head
*

Joined: 15 Mar, 2008
Posts: 13

Thank you I think I get the idea.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:01PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month