Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Upside Down Triangle

  • 01-12-2011 1:45pm
    #1
    Closed Accounts Posts: 95 ✭✭


    I've written a program which draws a triangle

    *
    **
    ***
    ****
    *****

    Now I need to reverse it, so it looks like

    *****
    ****
    ***
    **
    *

    below is my program but it just prints
    **************************************************************************************************************************************************************************************************************************************** ad infinitum.

    What am I doing wrong?
    import javax.swing.JOptionPane; //import JOptionPane
    class triangle1 {
        public static void main(String[] args) {
    String asterix = "*";
    String number=JOptionPane.showInputDialog(null, "Enter triangle size:"); //Get number1 using JOptionPane
    int n = Integer.parseInt(number); //convert number string to n int
    	 for (int i = n; i <= n; i--) {
     
         for (int j = n; j <= i; j--)
         {      System.out.print("*");
    	 }
     
           // print a new line
           System.out.println();
       }
    }
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Your condition is wrong.

    Maybe something like this:

    [php]
    for (int i = n; j > 0; i--) {
    // i will be n, n-1, n-2 etc
    for (int j = 0; j < i ; j++)
    {
    // this loop should print horizontally
    System.out.print("*");
    }
    }

    [/php]



    so they enter n = 5.

    The outer loop (i will go around 5 times, 5 lines)
    It will go from 5 down to 0.
    So if you want to print out 5 stars first, (i), then you do a normal loop upt to i.

    Remember for a for loop, set the initial condition, do the following while the condition (middle part) is true, and then after, modify the variable (i++) etc. Go over the loop like a computer would and you'll get the idea. You should know why it printed out all those *'s for infinity.


  • Closed Accounts Posts: 95 ✭✭The Crab


    Webmonkey wrote: »
    Your condition is wrong.

    Maybe something like this:

    [php]
    for (int i = n; j > 0; i--) {
    // i will be n, n-1, n-2 etc
    for (int j = 0; j < i ; j++)
    {
    // this loop should print horizontally
    System.out.print("*");
    }
    }

    [/php]



    so they enter n = 5.

    The outer loop (i will go around 5 times, 5 lines)
    It will go from 5 down to 0.
    So if you want to print out 5 stars first, (i), then you do a normal loop upt to i.

    Remember for a for loop, set the initial condition, do the following while the condition (middle part) is true, and then after, modify the variable (i++) etc. Go over the loop like a computer would and you'll get the idea. You should know why it printed out all those *'s for infinity.

    Thanks, now I have this but it produces no output except for JOptionPane
    import javax.swing.JOptionPane; //import JOptionPane
    class triangle {
        public static void main(String[] args) {
    String asterix = "*";
    String number=JOptionPane.showInputDialog(null, "Enter triangle size:"); //Get number1 using JOptionPane
    int n = Integer.parseInt(number); //convert number1 string to n int
    for (int i = n; i > 0; i--) {
    // i will be n, n-1, n-2 etc
    for (int j = 0; j < i ; j++)
      {      
         // this loop should print horizontally
         System.out.print("*");
      }
    }
    }
    }
    


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Well that cant be right, I just tested it and it does produce output...


  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    [PHP]
    int n = 5;

    // Print N number of rows
    for (int rows = 0; row < n; ++rows)
    {
    // Print N number of stars, decrement the number of stars each row
    for (int stars = (n - rows); --stars >= 0;)
    {
    System.out.print ("*");
    }
    }
    [/PHP]

    Haven't tested it but it might be easier to think of it this way.

    Using I's and J's for variable names looks compact but it doesn't help you read or solve the problem.

    The other code posted will be more efficient but such a small difference is negligible, it's much better to code for readability.


  • Closed Accounts Posts: 95 ✭✭The Crab


    I got it sorted based on your advice thanks.


  • Advertisement
Advertisement