上了几年学,才发现自己真的很差,居然连一个打印菱形图案都不能单独完成,耗费了大半个上午才调出两种方法。
1
/*
2 *
3 * 打印菱形图案
4 *
5 * */
6
7 import java.io.BufferedReader;
8 import java.io.InputStreamReader;
9
10 public class Diamond
11 {
12 public static void main (String[] args)
13 throws java.io.IOException
14 {
15 BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
16 System.out.println( " 请输入要打印菱形的行数(奇数) " );
17 int n = Integer.parseInt(br.readLine()) / 2 + 1 ; // 算出上半部分的总数
18
19 for ( int i = 1 ; i <= n; i ++ ) // 打印上半部分
20 {
21 for ( int j = 1 ; j <= n - i; j ++ )
22 {
23 System.out.print( " " );
24 }
25 for ( int k = 1 ; k <= 2 * i - 1 ; k ++ )
26 {
27 System.out.print( " * " );
28 }
29 System.out.println( " \n " );
30 }
31
32 for ( int i = 1 ; i < n; i ++ )
33 {
34 for ( int j = 1 ; j <= i; j ++ )
35 {
36 System.out.print( " " );
37 }
38 for ( int k = 1 ; k <= ( 2 * n - 1 ) - 2 * i; k ++ )
39 {
40 System.out.print( " * " );
41 }
42 System.out.println( " \n " );
43 }
44 }
45 }
2 *
3 * 打印菱形图案
4 *
5 * */
6
7 import java.io.BufferedReader;
8 import java.io.InputStreamReader;
9
10 public class Diamond
11 {
12 public static void main (String[] args)
13 throws java.io.IOException
14 {
15 BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
16 System.out.println( " 请输入要打印菱形的行数(奇数) " );
17 int n = Integer.parseInt(br.readLine()) / 2 + 1 ; // 算出上半部分的总数
18
19 for ( int i = 1 ; i <= n; i ++ ) // 打印上半部分
20 {
21 for ( int j = 1 ; j <= n - i; j ++ )
22 {
23 System.out.print( " " );
24 }
25 for ( int k = 1 ; k <= 2 * i - 1 ; k ++ )
26 {
27 System.out.print( " * " );
28 }
29 System.out.println( " \n " );
30 }
31
32 for ( int i = 1 ; i < n; i ++ )
33 {
34 for ( int j = 1 ; j <= i; j ++ )
35 {
36 System.out.print( " " );
37 }
38 for ( int k = 1 ; k <= ( 2 * n - 1 ) - 2 * i; k ++ )
39 {
40 System.out.print( " * " );
41 }
42 System.out.println( " \n " );
43 }
44 }
45 }