(1)常规的大星
*****
*****
*****
*****
上面的java程序为
1 public class daxing { 2 3 public static void main(String[] args) { 4 5 for (int i = 0; i < 5; i++) { 6 for (int j = 0; j < 5; j++) { 7 System.out.print("*"); 8 } 9 System.out.println(); 10 } 11 12 } 13 14 }
(2)
*****
****
***
**
*
上面程序打印代码为
方法一
1 public class daxing { 2 3 public static void main(String[] args) { 4 5 int z = 5; 6 for (int i = 0; i < 5; i++) { 7 for (int j = 0; j < z; j++) { 8 System.out.print("*"); 9 } 10 System.out.println(); 11 z--; 12 } 13 } 14 }
方法二:(优化一下)
1 public class daxing { 2 3 public static void main(String[] args) { 4 5 int z = 0; 6 for (int i = 0; i < 5; i++) { 7 for (int j = z; j < 5; j++) { 8 System.out.print("*"); 9 } 10 System.out.println(); 11 z++; 12 } 13 } 14 }
方法三:(再优化一下)
1 public class daxing { 2 3 public static void main(String[] args) { 4 5 6 for (int i = 0; i < 5; i++) { 7 for (int j = i; j < 5; j++) { 8 System.out.print("*"); 9 } 10 System.out.println(); 11 } 12 } 13 }