Nested loop structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayesha
    New Member
    • Oct 2006
    • 1

    Nested loop structure

    hi
    i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

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

    I need a code to make above mentioned , in nested loop form.

    thanks and regards
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by ayesha
    hi
    i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

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

    I need a code to make above mentioned , in nested loop form.

    thanks and regards
    There are many ways of doing this:
    Code:
    public class Test {
    	public static void main (String []args) {
    		int i = 0;
    
    		for(int j = 1; j <= 5; j++) {
    			if(j == 1 || j == 5) {
    				i = 4;
    			}
    			else {
    				i = 1;
    			}
    			for(int x = 0; x < i; x++) {
    				System.out.print("**");
    			}
    			System.out.println();
    
    		}
    	}
    }

    Comment

    • fido1234
      New Member
      • Jan 2009
      • 2

      #3
      Originally posted by ayesha
      hi
      i am beginner in java and i need help with programining ,Please if you could suggest me some sites where i can see some solved examples of codes as well as i need code for following

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

      I need a code to make above mentioned , in nested loop form.

      thanks and regards
      same as his question...do u hve a code for this one:
      *
      * *
      * * *
      * *
      *

      ty...

      must be look like a diamond...

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        We no longer hand out boilerplate codes. What have you tried so far?

        Comment

        • fido1234
          New Member
          • Jan 2009
          • 2

          #5
          Originally posted by r035198x
          We no longer hand out boilerplate codes. What have you tried so far?
          i tried to edit the code above but i cant come up with the result i want...im just a beginner to this language hope you can help me ty

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Don't try to edit that code. Start with a for loop and start printing out the stars with spaces at the correct location. Edit that code to so it gets as close to what is required as possible. Keep on editing and testing until you get the right picture.
            That is the best way to learn the language. Make sure you understand the results of each edit that you make.

            Comment

            • kamalFromEgypt
              New Member
              • May 2010
              • 2

              #7
              Here is the code

              public static void main(String[] args) {

              int flag;
              for (int i = 1; i <= 5; i++) {
              flag=i;
              if (i==4) {
              flag=2;
              } else if (i==5){
              flag=1;
              }

              for (int j = 0; j < flag; j++) {
              System.out.prin t("*");
              }

              System.out.prin tln();

              }

              Comment

              • kamalFromEgypt
                New Member
                • May 2010
                • 2

                #8
                *
                ****
                ******
                ********
                **********
                ************
                **************
                *************** *
                *************** ***
                *************** *****
                *************** *******


                public class Pyramid {

                /**
                * @param arg
                */
                public static void main(String arg[]){

                char prefix=' ';
                int lines=20;
                int spaces=45;

                for (int i=0;i<=lines;i+ +)
                {


                for(int j=spaces; j>0; j--)
                System.out.prin t(prefix);

                if(i==0)
                { System.out.prin t("*");
                System.out.prin t('\n');
                lines--;
                spaces--;
                continue;
                }

                for(int k=0;k<=i;k++)
                System.out.prin t("*");

                for(int k=0;k<=i;k++)
                System.out.prin t("*");


                System.out.prin t('\n');
                lines--;
                spaces--;
                }





                }


                }

                Comment

                Working...