public class asterisk
{
    public static void main(String[] args)
    {
        int row, col, spc=3, spc1=2, spc2=1, spc3=0;
        for(row=1;row<=1;row++)
        {
            for(col=1;col<=1;col++)
            {
                for(spc=spc;spc>0;spc--)
                {
                    System.out.print(" ");
                }
                System.out.print("*");
                System.out.println();
                {
                    for(spc1=spc1;spc1>0;spc1--)
                    {
                        System.out.print(" ");
                    }
                    System.out.print("**");
                    System.out.println();
                        {
                            for(spc2=spc2;spc2>0;spc2--)
                            {
                                System.out.print(" ");
                            }
                                System.out.print("***");
                                System.out.println();
                                {
                                    for(spc3=spc3;spc3>00;spc3--)
                                    {
                                        System.out.print(" ");
                                    }
                                        System.out.print("****");
                                        System.out.println();
                                }
                        }
                }
            }
        }
    }
}

i have this code that displays..

   *
  **
 ***
****

but how do this display with just using one asterisk each.. you see i have diff. count of asterisk in each for..

Recommended Answers

All 8 Replies

you're making this ... way too difficult.
you need two nested for - loops, that's all.
the outer one, is to make sure you have 5 lines (i < 5 )
the inner one, is to make sure you print the number of 's you need in one line, and that way, you can do it with having just one '' in your print statement.

i'm already confused thinking about this from morning.. -.-
i'll try using 2 nested for - loops then i'll just post here if I do it right or I have any errors..
btw thanks.. :)

i ended up using 3 nested for-loops..

well, show your code here
might be I can find something that can be improved.

public class NewClass 
{
    public static void main(String [] args)
    {
        for (int row = 1; row <= 4; row++)
        {
            for (int col = row ;col < 4; col++)
            {
                System .out.print(" ");
            }
                for (int spc = row; spc > 0; spc--)
                {
                    System .out.print("*");
                }
                    System .out.println(" ");
        }
        System .out.println("");
    }
}

here it is.. sorry for late reply.. :))

no problem :)
actually, for the spaces, you don't need a separate loop. you can deduct all the info (space or asterisk) from the nr of the second iteration, and based on the result of a simple if-statement, print an * or a space, like this:

public static void printStars( int length){


                String output = "";
                for( int i = 1; i < length; i++){
                    output = "";
                    for ( int j = 0; j < length; j++ ){
                        if ( j < (length-i))
                            output +=" ";
                        else
                            output +="*";
                    }               
                    System.out.println(output);
                }
            }

why it doesn't work when i tired to run it on JCreator?..

java.lang.NoSuchMethodError: main
Exception in thread "main" 

this is the error when i try to run it..

that means that the class you try to run doesn't contain a (valid) main method, and won't run, no matter how you try, or which IDE you use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.