# CS61B HW0 - Drawing a Triangle ###### tags: `Java` `Practice` Creative Exercise 1a: Drawing a Triangle Draw Triangle using while ``` package cs61b; public class DrawTriangle { public static void main(String[] args) { int row = 0; int SIZE = 5; while (row < SIZE) { int col = 0; while (col <= row) { System.out.print("*"); col = col + 1; } System.out.println(); row = row + 1; } } } ``` 1. column每loop一次都要先歸零,才會在首項打點 2. 換行之後要先print新的一行,row才能加1以開啟新的row 執行步驟展示: https://cscircles.cemc.uwaterloo.ca/java_visualize/#mode=edit --- Creative Exercise 1b: method "drawTriangle" 將畫三角形變成一個方法讓main去呼叫 ``` package cs61b; public class DrawTriangleMethod { public static void drawTriangle (int N) { int row = 0; int SIZE = N; while (row < SIZE) { int col = 0; while (col <= row) { System.out.print("*"); col ++; } System.out.println(); row++; } } public static void main(String[] args) { drawTriangle(10); } } ``` 將size的整數寫在argument中, 之後在呼叫的時候即可直接keyin整數