Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Loop Patterns (triangles) [Beginner]

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Loop Patterns (triangles) [Beginner]

    Write a Java program that prints one of the following patterns based on the user request:
    For an odd number of rows each pattern would look as follows (in this case 5 rows)
    54321
    5432
    543
    54
    5

    ____1
    ___12
    __123
    _1234
    12345

    12345
    _ 1234
    __123
    ___12
    ____1

    ___1
    _123
    12345
    _123
    ___1

    For an even number of rows each pattern would look as follows (in this case 4 rows)
    4321
    432
    43
    4

    ___1
    __12
    _123
    1234

    1234
    234
    34
    4

    _1
    123
    123
    _1

    this is my current assignment
    i'm having trouble getting started
    just to make it clear, i'm not looking for anyone to give me the complete program or anything.
    i just need a few hints on how to start it, what i should be using, etc (such as what kind of loops, how the get the numbers in that pattern, etc)
    any suggestions would be greatly appreciated
    (sorry if i'm not specific enough, let me know if more information is needed)


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Loop Patterns (triangles) [Beginner]

    If you need to read in user input from the console then take a look at the Scanner class.

    http://www.javaprogrammingforums.com...ner-class.html

    Your going to need to use 'for loops' for this also

    http://www.javaprogrammingforums.com...ava-loops.html

    Take a look at those examples and write some code. Post it here and we will help you move forward...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loop Patterns (triangles) [Beginner]

    import java.util.Scanner;
    public class Triangles {
     
        public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
     
            int pattern;    
     
            System.out.println(" - - - Welcome to Bhaswar's Traingle/Diamond Patterns - - - ");
            System.out.println("Which pattern do you want to print?");
            System.out.println("1) 54321   2)     1   3) 12345   4)   1");
            System.out.println("   4321          12      1234        123");
            System.out.println("   321          123      123        12345");
            System.out.println("   21          1234      12          123");
            System.out.println("   1          12345      1            1");
            System.out.print("Enter your choice (5 to quit):");
        while ((pattern = keyboard.nextInt()) >= 1 && pattern <= 5)
        {
            switch (pattern)
            {
            case 1:
                System.out.print("How many rows would you like to print? More than 1 please:");
                 int r1 = keyboard.nextInt();
     
                 for (int i=1; i<=r1; i++)
                 {
                     for (int j=r1; j>=i; j--)
                     {
     
                            System.out.print("" +j);}
                            System.out.print("\n");
            }
            }
     
        }
     
        }
    }
    this is all i currently have
    with a bit of mucking about i've gotten the numbers for the first triangle to look like:
    54321
    5432
    543
    54
    5
    unfortunately thats not what i'm trying to get
    i'm aiming for
    54321
    4321
    321
    21
    1

    any suggestions? i can't figure out how to get the 2nd second line starting from 4 and the 3rd line starting from 3 and so on...
    Last edited by me1010109; October 24th, 2010 at 07:03 PM.

Similar Threads

  1. Java Beginner Here!
    By j3nn42o in forum The Cafe
    Replies: 10
    Last Post: January 10th, 2011, 04:57 AM
  2. [SOLVED] While Loop Help (beginner)
    By Perplexing in forum Loops & Control Statements
    Replies: 4
    Last Post: October 23rd, 2010, 03:00 PM
  3. Basic Beginner Help
    By SRD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 05:27 PM
  4. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 08:22 AM