Results 1 to 11 of 11
  1. #1
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default I have to write a program to display triangles

    I have to write program where it has to do this: user inputs number
    print out lines with * on it, and it goes on until done. so if user inputs 7, it prints,
    Java Code:
    *
    **
    ***
    ****
    *****
    ******
    *******
    and i need to do it backward, pyramid and stuff, but if i can get this then i think I can do it like that. My code so far=
    Java Code:
       
          public static void standard(int n)
          { 
    for(int y=0;y<n;y++)
    {
          for(int x=0;x<n;x++)
          {
          System.out.print("*");
         }
    System.out.println();
    }
          }
    Last edited by Wizard0860; 02-14-2013 at 06:12 PM. Reason: code tags added, title changed

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: I have to write a program to display triangles

    I've added "code" tags to make the post readable. The idea is that you put [code] at the start of any code and [/code] at the end so that the indentation is preserved. This is also handy for anything where formatting is important.

    A bug in the forum software means that you should not use the word "print" as part of the title of a thread - especially near the end. Honestly! What happens is that the title gets converted into a URL, then when someone else clicks on the link the "print" at the end of the url is interpreted as meaning that the forum should display a print version of the thread.

    ---

    You might want to consider using a *pair* of nested loops for standard(). That's because there are two different things to be done over and over again:

    * Repeatedly draw lines (to make the triangle)
    * Repeatedly draw * (to make a line)

  3. #3
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: I have to write a program to display triangles

    so i know i need to make a loop that will controll the # of rows, but another loop and if statements inside that for the asterisks, but idk how to. plz explain more detail.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: I have to write a program to display triangles

    Why do you say you'll need if statements?

    Begin with the second construction and use a for loop to print a row of asterisks followed by a newline. Once you have that code functioning correctly you can put the whole thing inside of a second for loop to produce the different length lines. But that comes later. For now concentrate on a single for loop to produce a single line.

    If you get stuck, post code - saying what it does as well as what you expected or intended it would do. I would suggest removing all the other stuff so that others here can see exactly the for loop and the problems you are having with it.

  5. #5
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: I have to write a program to display triangles

    Ok so what i have so far is
    Java Code:
    for(int x=0;x<n;x++)
    {
    for(int y=0;y<n;y++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    and what that gives me is, if the user input is 4,
    Java Code:
    ****
    ****
    ****
    ****

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: I have to write a program to display triangles

    Imagine Java has a printLine(char c, int n) method that prints character c, n times. Printing a triangle is easy then:

    Java Code:
    printLine('*', 1);
    printLine('*', 2);
    printLine('*', 3);
    printLine('*', 4);
    printLine('*', 5);
    printLine('*', 6);
    printLine('*', 7);
    this clumsy code begs for a loop:

    Java Code:
    for (int n= 1; n <= 7; n++)
       printLine('*', n);
    now all you have to do is implement the printLine( ... ) method because Java doesn't have any ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: I have to write a program to display triangles

    Java Code:
    for(int y=0;y<n;y++)
    {
    System.out.print("*");
    }
    how many times is this printing *?

    what is n?

    and how do you fix it.
    Last edited by lenois; 02-14-2013 at 09:19 PM.

  8. #8
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: I have to write a program to display triangles

    Quote Originally Posted by lenois View Post
    Java Code:
    for(int y=0;y<n;y++)
    {
    System.out.print("*");
    }
    how many times is this printing *?

    what is n?

    and how do you fix it.
    This, theoretically, prints the character n times. In mathematics and programming, n is just as i a very common variable to assign numbers to.
    Ex. you count up 1, 2, 3, ..., n which of course can be inevitably long!

    Try to give n a value and see what happens!

  9. #9
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: I have to write a program to display triangles

    Ok so the code was actually this whole program. thats just fragment i need. JosAh if this helps could you clear up for me how to implement the method? i need this done by tomorrow. thanks
    Java Code:
       //Name______________________________ Date_____________
       import javax.swing.JOptionPane;
       public class Driver12
       {
          public static void main(String[] args)
          {
             while(true)
             {
                int n = Integer.parseInt(
                           JOptionPane.showInputDialog(
                                        "How many rows? (-1 to quit)"));
                if(n == -1)
                {
                   System.out.println("Bye-bye!");
                   System.exit(0);
                }
                String message = "What type? (1-4)";
                message = message + "\n1. Standard";
                message = message + "\n2. Backward";
                message = message + "\n3. Pyramid";
                message = message + "\n4. Box with X";
                int type = Integer.parseInt(JOptionPane.showInputDialog(message));
                switch(type)
                {
                   case 1: standard(n);
                      break;
                   case 2: backward(n);
                      break;
                   case 3: pyramid(n);
                      break;
                   case 4: box(n);
                      break;
                   default: System.out.println("Not a valid type.");
                      break;
                }
                System.out.println();
             }
          }
          public static void standard(int n)
          {
             public static void standard(int n)
          { 
    for(int y=0;y<n;y++)
    {
          for(int x=0;x<n;x++)
          {
          System.out.print("*");
         }
    System.out.println();
    }
          }
          }
          public static void backward(int n)
          {
             System.out.println("This type is not currently supported.");
          }
          public static void pyramid(int n)
          {
             System.out.println("This type is not currently supported.");
          }
          public static void box(int n)
          {
             System.out.println("This type is not currently supported.");
          }
       }

  10. #10
    Wizard0860 is offline Member
    Join Date
    Nov 2012
    Posts
    29
    Rep Power
    0

    Default Re: I have to write a program to display triangles

    Sorry for double posting, but I think I got it.
    Java Code:
    public static void printStars(int number)
          {
             for(int r = 0; r < number; r++)
             {
                System.out.print("*");
             }
          }
    public static void standard(int n)
          {
             for(int x = 0; x <= n; x++)
             {
                printStars(x);
                System.out.println("");
             }
          }

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,019
    Blog Entries
    7
    Rep Power
    20

    Default Re: I have to write a program to display triangles

    Well done; you found the solution yourself (it's much better than being spoonfed); as you can see, splitting to problem in several simple methods really 'unravels' the problem to just a bunch of trivial steps; that's the way to do it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 7
    Last Post: 11-30-2011, 06:58 PM
  2. Write a program..??
    By QSilver in forum New To Java
    Replies: 7
    Last Post: 02-21-2011, 02:20 PM
  3. trying to write a program for this
    By durdanto in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 01:27 PM
  4. how can write program for gui
    By pothireddy in forum Advanced Java
    Replies: 9
    Last Post: 11-04-2010, 06:17 AM
  5. Trying to write my own program : /
    By jdicerch in forum New To Java
    Replies: 50
    Last Post: 09-26-2010, 01:44 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
  •