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 2 of 2

Thread: Funky triple nested loop problem. (Due tonight)(Confusing)

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Location
    Kentucky
    Posts
    11
    My Mood
    Worried
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Funky triple nested loop problem. (Due tonight)(Confusing)

    I have this assignment that's due tonight at midnight, but I have completed all but 1 part. The second triangle, I am having problems grasping the concept of nested loops I believe, but it's part of it. Anyways, here is question then code.

    Write a Java program that reads positive integer n, 1 ≤ n ≤ 13, and plots 3 triangles of size n as shown below.

    For n = 4, for instance, the program should plot: (works for n = 5 and n = 6 etc.)



    triangle 1

    1

    2 3

    4 5 6

    7 8 9 10



    triangle 2

    _ _ _ 1

    _ _ 3 2

    _ 6 5 4

    10 9 8 7



    triangle 3

    _ _ _ 1

    _ _ 3 3 3

    _ 5 5 5 5 5

    7 7 7 7 7 7 7

    import java.util.Scanner;
    class Loops {
     
     
        void plotTriangle1(int n) {
            int t = 1;
            for (int i = 1; i <= n; i++) {
                System.out.println(" ");
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%3d", t++);
     
                }
                System.out.println();
            }
        }
     
        void plotTriangle2(int n) {
            int t = 1;
            for (int i = 1; i <= n; i++) {
     
                for (int k = 1; k <= i; k++) {
     
                }
                for (int j = 1; j <= i; j++) {
     
                    System.out.printf("%3d",t++);
     
                }
                System.out.println();        
            }
     
     
        } 
     
     
        void plotTriangle3(int n) {
     
            for (int i = 1; i <= n; i++) {
                System.out.println(); 
                for(int j = 1;j <= (n-i);j++) {
                    System.out.print("   ");
                }
                for (int j = 1; j <= 2*i-1; j++) { 
                    System.out.print(" "); 
                    System.out.printf("%3d",2*i-1);
                }
                System.out.println();
            }
        }   
    }
     
    public class Lab7 {
     
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);  
            Loops myL = new Loops();
     
            int n;
     
            System.out.print("Enter n(1-13):");
            n = in.nextInt();
     
            //myL.plotTriangle1(n);
            myL.plotTriangle2(n);
            //myL.plotTriangle3(n);
     
        }
     
    }

    Currently compiles to this: Enter n(1-13):4
    1
    2 3
    4 5 6
    7 8 9 10

    --- Update ---

    Sorry for the question format of the triangles, I couldnt get the spacing to work so I used underscores to replicate the blank space on the forum. Thanks.
    Last edited by goodtanner; October 10th, 2014 at 06:27 PM. Reason: showing compile


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Funky triple nested loop problem. (Due tonight)(Confusing)

    What problems are you having getting the program to work? Do you have any specific questions?

    NOTE: Put the output in code tags to preserve its formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. HourGlass [Due tonight need help!]
    By iSuckAtJava in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 1st, 2014, 04:38 AM
  2. Project Nursing Home Due Tonight Help
    By morrism35 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 16th, 2013, 07:17 PM
  3. Using FOR and WHILE loops to find average (DUE TONIGHT)
    By cbh793 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 1st, 2013, 08:56 PM
  4. Replies: 1
    Last Post: December 5th, 2012, 10:58 PM
  5. Help On Java Homework (DUE TONIGHT!!!)
    By agmolina90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2011, 08:28 AM