Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    yaseen is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default How to print this in java?

    How to print this in java?


    1
    2 1
    1 2 3
    4 3 2 1
    1 2 3 4 5
    6 5 4 3 2 1

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    195
    Rep Power
    5

    Default

    1. This seems like entry level homework (since I did this)
    2. Wrong section.
    3. Research Loops
    4. This example should be in google
    5. System.out.println
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Sno View Post
    1. This seems like entry level homework (since I did this)
    2. Wrong section.
    Yep. Moving to New to Java section.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    One answer is to Use the println() statement. Code the data for the each line followed by the newline characters until you've got all the lines

    Or is your question, how to generate the data?

    Look at the data, line by line.
    Two things can be seen:
    The data gets longer for each line
    The order of the data reverses line by line

    Make a method that takes the line number and returns the data as needed for that line.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by yaseen View Post
    How to print this in java?


    1
    2 1
    1 2 3
    4 3 2 1
    1 2 3 4 5
    6 5 4 3 2 1
    Do you know how this pattern generated? And what you've tried so far?

  6. #6
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    Java Code:
    boolean reverse = false;
    for(int i=0; i<6;i++)
    {
         if(reverse)
         {
               for(int j=1, j<i; j++)
               {
                     System.out.print(j);
                     reverse=!reverse;
               }
          }
          else
          {
                  for(int j=i;  j>0; j--)
                  {
                          System.out.print(j);
                           reverse=!reverse;
                  }
          }
          System.out.println();//j was a typo sorry
    }
    Just post if you need an explanation
    Last edited by bayan; 05-16-2010 at 12:01 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You could always explain...why?

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    You could always explain...why?
    Don't be an unthankful rapscallion; be grateful for the spoonfed magical solution instead; copy and paste the beautiful gem and use it in utmost delight. Don't question the algorithm and don't ask for a why, it just came to us in merci, halleluya.

    kind regards,

    Jos ;-)

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by bayan View Post
    Just post if you need an explanation

    the used j in the last println is out of scope, since j was declared inside the else block. but it's a quick fix to correct and thx for the code.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by j2me64 View Post
    the used j in the last println is out of scope, since j was declared inside the else block. but it's a quick fix to correct and thx for the code.
    The loop boundaries are also shaky at best. Try to start with 'reverse= true' if you want the other pyramid; it doesn't work. It is crappy code.

    kind regards,

    Jos

  11. #11
    sduskis is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default

    In groovy, it would look like this:
    (1..6).each{
    ((it%2) ? (it..1) : (1..it)).each{print "$it ";}
    println ""
    }

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sduskis View Post
    In groovy, it would look like this:
    Why does it say "Java Forums" on top of each page?

    kind regards,

    Jos

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    The loop boundaries are also shaky at best. Try to start with 'reverse= true' if you want the other pyramid; it doesn't work. It is crappy code.

    kind regards,

    Jos
    But its free!

    ...or, yet another reason I hate code dumps. First, as you implied above, they don't help learning, and second they're often not all that good.

    And as for the Groovy example, that really...really...really isn't selling me on that language.

  14. #14
    sduskis is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default

    Points about Groovy Taken. However, there are 2 things to notice from the Groovy approach that ported, in various degrees of complexity to Java.

    1) reverse = !reverse can be accomplished with alternative means. if(i % 2 == 1) <do reverse> else <do forward>. That looks a bit ugly, but processing of "rows" based on the remainder function is a pretty common solution to this type of problem.

    2) Closures have been discussed in regards to Java for quite a while. The "how you iterate" vs. "what you do in the loop" is. In the java world, those two concepts are tightly coupled.

    Taking #1 into account, here's what the algorithm looks like:
    for(int i=1; i<=6;i++)
    {
    if(i%2==1)
    {
    for(int j=1; j<=i; j++)
    System.out.print(String.valueOf(j)+" " );
    }
    else
    {
    for(int j=i; j>0; j--)
    System.out.print(String.valueOf(j)+" " );
    }
    System.out.println();
    }
    It's important to note that #2 shouldn't be addressed in a Java world on a simple case like this one, but should be thought about for more complex scenarios.

  15. #15
    wzq110120 is offline Member
    Join Date
    May 2010
    Location
    china
    Posts
    7
    Rep Power
    0

    Default

    I can frome china,I want to make friend with you,My MSN is esxwazq123@hotmail.com
    My replay:

    int target = 6;
    for(int i = 1;i<=target;i++){

    if(i%2!=0){
    for(int j = 1; j<=i; j++)
    System.out.print(j);
    }else{
    for(int k= i; k>0; k--)
    System.out.print(k);
    }
    System.out.println();
    }


  16. #16
    svidurs1 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default here s it.

    A little more interactive. You can enter (at runtime) the number of lines for which you want to repeat this pattern.

    Java Code:
    import java.io.*;
    public class Pattern{
    	public static void main (String[] args) throws IOException{
    		System.out.print("Enter the number of lines for which you want this pattern.: \t");
    		int noOfLines = Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine());
    		for(int i=1; i<=noOfLines;i++)
    		{
    		     if((i%2) == 0)
    		     {
    		           for(int j=i; j>=1; j--)
    		           {
    		                 System.out.print(j + "  ");
    		           }
    		      }
    		      else
    		      {
    		              for(int j=1;  j<=i; j++)
    		              {
    		                      System.out.print(j + "  ");
    		              }
    		      }
    		      System.out.println();
    		}
    	}
    }
    Last edited by Eranga; 05-28-2010 at 03:00 PM. Reason: code tags added

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags when you are posting here in the forum. Unformated codes are really hard to read. If you don't know how to do it, check on my forum signature.

  18. #18
    svidurs1 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    Point taken. !!

    Java Code:
    import java.io.*;
    public class Pattern{
    	public static void main (String[] args) throws IOException{
    		System.out.print("Enter the number of lines for which you want this pattern.: \t");
    		int noOfLines = Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine());
    		for(int i=1; i<=noOfLines;i++)
    		{
    		     if((i%2) == 0)
    		     {
    		           for(int j=i; j>=1; j--)
    		           {
    		                 System.out.print(j + "  ");
    		           }
    		      }
    		      else
    		      {
    		              for(int j=1;  j<=i; j++)
    		              {
    		                      System.out.print(j + "  ");
    		              }
    		      }
    		      System.out.println();
    		}
    	}
    }

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    No need of doing that actually. I've already do that, keep in mind to do it next time onwards. :)

  20. #20
    svidurs1 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    yeah i know.. was just figuring out if its done this way !!
    sry for playin around !!

Page 1 of 2 12 LastLast

Similar Threads

  1. How to use and print the JAVA code from a JSP
    By Ginkan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-23-2010, 03:05 PM
  2. Print .prn file using java
    By nishant in forum Advanced Java
    Replies: 0
    Last Post: 07-14-2009, 10:15 AM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  4. print in java linux
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 05:24 AM
  5. print .doc in java
    By Alan in forum Advanced Java
    Replies: 1
    Last Post: 05-17-2007, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •