Matrix problem using loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • don bosco
    New Member
    • Apr 2014
    • 5

    #1

    Matrix problem using loops

    pattern--

    *-----
    **----
    ***---
    ****--
    *****-
    i tried here is what i have done please help me make it correct

    Code:
    class pattern
    {   
        public void display()
        {int i,j;
            
            for(i=1;i<=5;i++)
            {
                for(j=1;j<=i;j++)
                {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
        
        public void displayer()
        {int i,j;
        for(i=1;i<=5;i++)
            {
                for(j=5;j>=i;j--)
                {
                    System.out.print("-");
            }
              System.out.println();
            }
        public static void main(String arg[]);
    {
        pattern obj=new pattern();
        obj.display();
        obj.displayer();
    }
    }
    }
    Last edited by Rabbit; Apr 30 '14, 03:40 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code or formatted data.

    You're already not using an array. But why would you want to do it without using loops?

    Comment

    • don bosco
      New Member
      • Apr 2014
      • 5

      #3
      i want you to help me make it correct by using loops only and thank u for viewing my post plzz help me do it

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        You already are using loops. I don't understand your question.

        Comment

        • stdq
          New Member
          • Apr 2013
          • 94

          #5
          If you want to simplify, you could do everything in main without using classes. Just use two loops, and two counters - it might help you if you name them with complete words, like row and column. Let's analyze the pattern:

          * row 1 - column goes from 1 to 1, which equals row.
          ** row 2 - column goes from 1 to 2, which equals row.
          *** row 3 - column goes from 1 to 3, which equals row.
          **** row 4 - column goes from 1 to 4, which equals row.
          ***** row 5 - column goes from 1 to 5, which equals row.

          So, the limit for the inner for loop depends on the current value of the counter for the outer loop, and you named these counters j and i, respectively.

          Comment

          • don bosco
            New Member
            • Apr 2014
            • 5

            #6
            yes but i have to print the under scores in oppsite directions too

            Comment

            • don bosco
              New Member
              • Apr 2014
              • 5

              #7
              yes i am using loops but if you and run this program in java(blue j) then it does not give the desired effect rabbit. i have tried it but it only print either the stars or the underscores plzz make this correct to print the pattern above

              Comment

              • don bosco
                New Member
                • Apr 2014
                • 5

                #8
                i got my answer thank you for viewing my post
                thank you all

                Comment

                Working...