Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package treedraw;
- /**
- *
- * @author mute
- */
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- final int height = 9; //Tree height
- final int stemHeight = 2;
- //counters
- int count = 0;
- int whiteCount = 4;
- int drawCount = 1;
- int i;
- //Strings
- String drawSpace = "*";
- String whiteSpace = " ";
- while ( count <= (height - 2) ) {
- while ( whiteCount >= 0 ) {
- // for ( i = whiteCount; i >= 0; i-- ) //This loop adjust the whiteSpace ' ' String
- whiteSpace = whiteSpace.substring( 1, (whiteSpace.length()));
- for ( i = drawCount; i >= 0; i--) //This loop adjusts the drawSpace '*' String
- drawSpace += '*';
- System.out.println( whiteSpace + drawSpace + whiteSpace );
- drawCount++; //iterates the counters
- whiteCount--;
- count++;
- }
- }
- while ( (count > ( height - 2 )) && (count < height ) ) {
- drawCount = 3;
- whiteCount = 3;
- whiteSpace = " ";
- drawSpace = "*";
- //Resused for loop to draw stem rather than just write the characters...could be methodized later
- for ( i = whiteCount; i >= 0; i-- )
- whiteSpace += ' ';
- for ( i = drawCount; i >= 0; i-- )
- drawSpace += '*';
- for ( i = stemHeight; i >= 0; i--)
- System.out.println( whiteSpace + drawSpace + whiteSpace );
- }
- }
- }
- ////////////
- Throws this error.
- ////////////
- Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
- at java.lang.String.substring(String.java:1937)
- at treedraw.Main.main(Main.java:34)
- Java Result: 1
- BUILD SUCCESSFUL (total time: 1 second)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement