Free2Code
 
Time: 2008-11-20, 11:22pm
How 2 print out Pyramid?
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:
  1. //This is the output we suppose to get
  2.                    
  3.                  pyramid height: 1
  4.                                       *
  5.                                      * *


» Post edited 2006-09-20, 03:35am by what2do.

 
  Reply to this ·  Post link ·  Top
Subject: Re: How 2 print out Pyramid?  ·  Posted: 2007-05-02, 01:00pm
Rank: ? (142)
Member #: 6881
Try this: Code:
  1. //TriangleMaker....wooo!
  2. import javax.swing.*;
  3. public class TriangleMaker{
  4.         int height;
  5.         
  6.     public TriangleMaker(int height){
  7.         this.height = height;                
  8.         }
  9.         
  10.     public String duplicateString(String symbol, int num){
  11.         String t = "";
  12.         
  13.          if(symbol == null)
  14.              return "";
  15.          
  16.          for(int i = 0; i <= num; i++)
  17.              t += symbol+ " ";
  18.             
  19.         return t;    
  20.     }
  21.         
  22.     public String toString(){
  23.         String s = "Pyramid Height: " + height + "\n";
  24.         for(int i = 0; i <= height; i++){
  25.         
  26.             if( ((height-i) % 2) != 0)
  27.                 s += " ";
  28.                 
  29.             s += this.duplicateString(" ", (int)(height - i)/2 ) +  this.duplicateString("*",i) +"\n";
  30.         
  31.         }
  32.         return s;                
  33.       }        
  34.     
  35.       
  36.     public static void main(String[] args){
  37.         String n = JOptionPane.showInputDialog("Pyramid Height: ", "1");
  38.         int n1 = Integer.parseInt(n);
  39.         System.out.println(new TriangleMaker(n1));
  40.     }    
  41. }



I just noticed that the original post was made back in september....wooo! and what happened to the cool colored code blocks

Holla
 
  Reply to this ·  Post link ·  Top
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).
 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons