Loop Patterns (triangles) [Beginner]

• October 24th, 2010, 04:11 AM
me1010109
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)
• October 24th, 2010, 08:33 AM
JavaPF
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...
• October 24th, 2010, 05:13 PM
me1010109
Re: Loop Patterns (triangles) [Beginner]
Code Java:

```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...