how to make out the pyramid number? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to make out the pyramid number?

bananalccbananalcc Posts: 2Member
what kind of function should use on this pyramid number?


example:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Comments

  • zibadianzibadian Posts: 6,349Member
    : what kind of function should use on this pyramid number?
    :
    :
    : example:
    : 1
    : 2 2
    : 3 3 3
    : 4 4 4 4
    : 5 5 5 5 5
    :
    This looks like a nested for-loop with some string formatting applied. Here's an example code:
    [code]
    for (int i = 1; i < 6; i++) {
    String result = "";
    for (int j = 1; j < i+1; j++) {
    result = result + " " + i;
    }
    System.out.println(result);
    }
    [/code]
  • bananalccbananalcc Posts: 2Member
    : This looks like a nested for-loop with some string formatting
    : applied. Here's an example code:
    : [code]:
    : for (int i = 1; i < 6; i++) {
    : String result = "";
    : for (int j = 1; j < i+1; j++) {
    : result = result + " " + i;
    : }
    : System.out.println(result);
    : }
    : [/code]:

    what the String work at here? can explain for me? im so sorry.. i just learn java and this question is my pratical question.. and im not so understand what the lecturer teaching...
  • zibadianzibadian Posts: 6,349Member
    : : This looks like a nested for-loop with some string formatting
    : : applied. Here's an example code:
    : : [code]: :
    : : for (int i = 1; i < 6; i++) {
    : : String result = "";
    : : for (int j = 1; j < i+1; j++) {
    : : result = result + " " + i;
    : : }
    : : System.out.println(result);
    : : }
    : : [/code]: :
    :
    : what the String work at here? can explain for me? im so sorry.. i
    : just learn java and this question is my pratical question.. and im
    : not so understand what the lecturer teaching...
    :
    The String variable is there to store each line as it is being created in the inner loop. Since it there is no single statement to dynamically create the lines, you'll need some storage space to build the numbers. The easiest in this case is a String. Other data structures are also possible, but the the output becomes more difficult to generate.
Sign In or Register to comment.