Time: 2008-11-20, 11:22pm
Please
login or
register to post a reply.
|
|
| Subject: How 2 print out Pyramid? · Posted: 2006-09-20, 03:30am |
|
|
|
Rank: ? (5)
Member #: 28493
|
Hey, anyone knows how to use standard input and output to get this thing.. Using duplicateString();
For example, duplicateString("*", 5) returns the string "*****", duplicateString(" ", 3) returns a string with three spaces;
Code:
//This is the output we suppose to get
-
-
- pyramid height: 1
-
- *
- * *
» Post edited 2006-09-20, 03:35am by what2do.
|
|
|
| Subject: Re: How 2 print out Pyramid? · Posted: 2007-05-02, 01:00pm |
|
|
|
Rank: ? (142)
Member #: 6881
|
Try this: Code: - //TriangleMaker....wooo!
- import javax.swing.*;
- public class TriangleMaker{
- int height;
-
- public TriangleMaker(int height){
- this.height = height;
- }
-
- public String duplicateString(String symbol, int num){
- String t = "";
-
- if(symbol == null)
- return "";
-
- for(int i = 0; i <= num; i++)
- t += symbol+ " ";
-
- return t;
- }
-
- public String toString(){
- String s = "Pyramid Height: " + height + "\n";
- for(int i = 0; i <= height; i++){
-
- if( ((height-i) % 2) != 0)
- s += " ";
-
- s += this.duplicateString(" ", (int)(height - i)/2 ) + this.duplicateString("*",i) +"\n";
-
- }
- return s;
- }
-
-
- public static void main(String[] args){
- String n = JOptionPane.showInputDialog("Pyramid Height: ", "1");
- int n1 = Integer.parseInt(n);
- System.out.println(new TriangleMaker(n1));
- }
- }
I just noticed that the original post was made back in september....wooo! and what happened to the cool colored code blocks
|
|
|
| Subject: Re: How 2 print out Pyramid? · Posted: 2007-06-02, 12:24pm |
|
|
|
Rank: ? (383)
Member #: 867
|
StringBuilder may be a more efficient solution if your temporary string is going to be appended to several times (as in uni's code).
|
Pages: 1
Please
login or
register to post a reply.