打印一排*,很簡單,打印下圖

也很簡單,代碼如下:
1 public class Work10_3 {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 int a=0;
9 while(a<4){
10 int i=0;
11 while(i<10){
12 System.out.print("*");//打印*不換行
13 i++;
14 }
15 System.out.println("");//換行
16 a++;
17 }
18 }
19 }
可是昨天想了好久都沒想到怎樣做到下面圖片的樣子,今天突然就有了靈感

代碼很簡單,就是昨天想破了腦袋都想不出來,好笨啊我
第一行打印一個*,第二行行打印兩個*,大三行打印三個*,這樣分析就找到規律了,定義一個a=1,外層循環實現打印幾行,定義一個i=0,
實現內層循環打印*,當a=1時是第一行,想讓內層打印一個*,那麼內層循環條件是i<1,這樣就打印一個*,當a=2時,是第二行,想讓內
層打印兩個*,那麼內層就是i<2,這樣又不難看出i<a,於是代碼如下:
1 public class Work10 {
2 /**
3 * @param args
4 */
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 int a=1;
8 while(a<25){
9 int i=0;
10 while(i<a){
11 System.out.print("*");//打印*不換行
12 i++;
13 }
14 System.out.println("");//換行
15 a++;
16 }
17 }
18 }
很簡單的代碼,還可以改進一下。
有了一個靈感之後,就不能浪費,要充分鍛煉自己的才能,
於是我又打印了一條斜線

這是往右斜著的
讓內層打印空格(和上面內層打印*一樣),外層打印一個*,和剛才外層有些微小區別
代碼如下:
1 public class Work10_1 {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 int a=1;
9 while(a<25){
10 int i=0;
11 while(i<a){
12 System.out.print(" ");//打印空格不換行
13 i++;
14 }
15 System.out.print("*\n");//打印*後換行
16 a++;
17 }
18 }
19
20 }

這是往左斜著的,內層我定義i=25,a=1時打印24個空格,然後打印*換行,當a=2時,打印23個空格,然後打印*換行。。。。。代碼如下:
1 public class Work10_2 {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 int a=1;
9 while(a<25){
10 int i=25;
11 while(i>a){
12 System.out.print(" ");//打印空格不換行
13 i--;
14 }
15 System.out.print("*\n");//打印*後換行
16 a++;
17 }
18 }
19
20 }
只有做不到的,沒有想不到的,看下圖

和上面一樣的方法,一個內層while打印空格,另一個打印*,
這個也很簡單,要是之前的我肯定不會這麼說,現在知道怎麼做了,就感覺簡單了,
代碼如下:
1 public class Work10_4 {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 int a=1;
9 while(a<25){
10 int i=25;
11 while(i>a){
12 System.out.print(" ");//打印空格不換行
13 i--;
14 }
15 int b=0;
16 while(b<a){
17 System.out.print("*");//打印*不換行
18 b++;
19 }
20 System.out.println("");//換行
21 a++;
22 }
23 }
24
25 }
收回剛才的話啊 下面這個我沒想到,就是改了下代碼就變這樣了,其實我想做一個等腰三角形的

代碼如下:
1 public class Work10_5 {
2 public static void main(String[] args) {
3 // TODO Auto-generated method stub
4 int a=0;
5 while(a<25){
6 int i=25;
7 while(i>a){
8 System.out.print(" ");//打印空格不換行
9 i--;
10 }
11 int b=0;
12 while(b<a){
13 System.out.print("*");
14 b+=2;//這裡和上面的不一樣
15 }
16 System.out.println("");
17 a++;
18 }
19 }
20 }
這個等腰三角形不好做啊 試了幾次沒有做出來,我要在定義一個變量試一試…
原來真是需要添加一個變量,添加以後瞬間就做出來了

分析一下,第一行先打印好多空格,然後一個*,第二行空格減少一個,*增加兩個,因為增加的速度不一樣,所以需要兩個變量分別控制兩個內層循環,空格的打印和之前的都一樣, 只是打印*的速度要增加,代碼如下:
1 // TODO Auto-generated method stub
2 int a=1;
3 int c=1;
4 while(a<25){
5 int i=25;
6 while(i>a){
7 System.out.print(" ");
8 i--;
9 }
10 int b=0;
11 while(b<c){
12 System.out.print("*");
13 b++;
14 }
15 System.out.println("");
16 a++;
17 c+=2;
18 }
19
然後我又想打印一個倒三角,然後很輕松就打印出來了

這個就不多說了,代碼如下:
1 public class Work10_6 {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 int a=1;
9 int c=1;
10 while(a<25){
11 int i=25;
12 while(i>a){
13 System.out.print(" ");
14 i--;
15 }
16 int b=0;
17 while(b<c){
18 System.out.print("*");
19 b++;
20 }
21 System.out.println("");
22 a++;
23 c+=2;
24 }
25 }
26
27 }
還能把這寫三角形組合起來,有多種組合方式,下面提供一種做參考:

代碼如下:
1 public class Work10_13 {
2
3 /**
4 * @param args
5 */
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 int a=1;
9 int c=1;
10 while(a<20){
11 int i=20;
12 while(i>a){
13 System.out.print(" ");
14 i--;
15 }
16 int b=0;
17 while(b<c){
18 System.out.print("*");
19 b++;
20 }
21 System.out.println("");
22 a++;
23 c+=2;
24 }
25 int d=0;
26 int e=39;
27 while(d<20){
28 int i=0;
29 while(i<d){
30 System.out.print(" ");//打印空格不換行
31 i++;
32 }
33 int b=0;
34 while(b<e){
35 System.out.print("*");
36 b++;
37 }
38 System.out.println("");
39 d++;
40 e-=2;
41 }
42 }
43 }
------------------------------------------------------------------------------------------------------------------------------------------------------------
轉載請注明出處!