双重循环打印顶点在左上的直角三角形:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public static void main(String[] args) { // TODO Auto-generated method stub int num= 0 ; System.out.println( "请输入直角三角形的行数:" ); Scanner input= new Scanner(System.in); num=input.nextInt(); for ( int i= 1 ;i<=num;i++){ for ( int k= 1 ;k<=num-i+ 1 ;k++){ System.out.print( "*" ); } System.out.print( "\n" ); } } |
打印顶点在左下的直角三角形
1
2
3
4
5
6
7
8
9
10
11
12
|
public static void main(String[] args) { int rows= 0 ; System.out.println( "请输入要求的三角形的行数:" ); Scanner input= new Scanner(System.in); rows=input.nextInt(); for ( int i= 1 ;i<=rows;i++){ for ( int j= 1 ;j<= 2 *i- 1 ;j++){ System.out.print( "*" ); } System.out.print( "\n" ); } } |
打印定点在右上的直角三角形
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static void main(String[] args) { int num= 0 ; System.out.println( "请输入三角形所需要的行数:" ); Scanner input= new Scanner(System.in); num=input.nextInt(); for ( int i= 1 ;i<=num;i++){ for ( int k=num;k>num-i+ 1 ;k--){ System.out.print( " " ); } for ( int j=num;j>=i;j--){ System.out.print( "*" ); } System.out.println(); } System.out.println(); } |
打印定点在右下的直角三角形
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static void main(String[] args) { int num= 0 ; System.out.println( "请输入打印的三角形行数:" ); Scanner input= new Scanner(System.in); num=input.nextInt(); for ( int i= 1 ;i<=num;i++){ for ( int k=num;k> 0 ;k--){ if (k<=i){ System.out.print( "*" ); } else { System.out.print( " " ); } } System.out.println(); } } |
打印顶点在上的等腰三角形:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public static void main(String[] args) { int num= 0 ; System.out.println( "请输入要打印三角形的行数:" ); Scanner input= new Scanner(System.in); num=input.nextInt(); for ( int i= 1 ;i<=num;i++){ for ( int k= 0 ;k<=num-i;k++){ System.out.print( " " ); } for ( int j= 1 ;j<= 2 *i- 1 ;j++){ System.out.print( "*" ); } System.out.print( "\n" ); } } |
打印顶点在下的等腰三角形
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public static void main(String[] args) { int num= 0 ; System.out.println( "请输入要求的三角形行数:" ); Scanner input= new Scanner(System.in); num=input.nextInt(); for ( int i= 1 ;i<=num;i++){ for ( int m=num- 1 ;m>num-i;m--){ System.out.print( " " ); } for ( int j=num;j>=i;j--){ System.out.print( "* " ); } System.out.println(); } } |
打印矩形
1
2
3
4
5
6
7
8
|
public static void main(String[] args) { for ( int i= 0 ;i< 5 ;i++){ for ( int j= 0 ;j< 5 ;j++){ System.out.print( "*" ); } System.out.print( "\n" ); } } |
打印空心菱形和实心菱形
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
public static void main(String[] args) { int hangshu = 11 ; int yiban = hangshu / 2 + 1 ; int yibanduo = hangshu / 2 ; System.out. println ( "空心菱形:" ); for( int k = 1 ;k <= yiban;k++){ for( int i = 1 ;i <= (yiban - k);i++){ System.out. print ( " " ); } System.out. print ( "*" ); for( int i = 1 ;i <= ( (k - 2 ) * 2 + 1 );i++){ System.out. print ( " " ); } if (k != 1 ){ System.out. print ( "*" ); } System.out. println (); } for( int k = yibanduo;k >= 1 ;k--){ for( int i = 1 ;i <= (yiban - k);i++){ System.out. print ( " " ); } System.out. print ( "*" ); for( int i = 1 ;i <= ( (k - 2 ) * 2 + 1 );i++){ System.out. print ( " " ); } if (k != 1 ){ System.out. print ( "*" ); } System.out. println (); } System.out. println ( "实心菱形:" ); for( int i = 1 ;i <= hangshu;i++){ if (i <= hangshu/ 2 + 1 ){ for( int k = 1 ;k <= hangshu/ 2 + 1 - i;k++){ System.out. print ( " " ); } for( int k = 1 ;k <= i;k++){ System.out. print ( "* " ); } System.out. println (); } else { for( int k = 1 ;k <= (i -(hangshu/ 2 + 1 ));k++){ System.out. print ( " " ); } for( int k = 1 ;k <= ( 2 *(hangshu/ 2 + 1 ) - i);k++){ System.out. print ( "* " ); } System.out. println (); } } } |
打印平行四边形
1
2
3
4
5
6
7
8
9
10
11
12
|
public static void main(String[] args) { for ( int i = 0 ; i < 5 ; i++) { //打印行 for ( int j = 0 ; j < 4 -i; j++) { //打印空格 System.out.print( " " ); //引号中间加空格 } for ( int j = 0 ; j < 6 ; j++) { //打印列 System.out.print( "*" ); } System.out.println(); //换行 } } |
打印九九乘法表
1
2
3
4
5
6
7
8
9
|
public static void main(String[] args) { int num= 9 ; for ( int i= 1 ;i<=num;i++){ for ( int j= 1 ;j<=i;j++){ System.out.print(j+ "*" +i+ "=" +j*i+ " " ); } System.out.println(); } } |
九九除法表
1
2
3
4
5
6
7
8
9
10
|
public class TestNineNine { public static void main(String[] args) { for ( int b= 1 ;b<= 9 ;b++) { for ( int a= 1 ;a<= 9 ;a++) { int c = a*b; System.out.print(c+ "/" +a+ "=" +b+ "\t" ); } System.out.println(); } } |
总结
以上就是本文关于Java编程之双重循环打印图形的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。
原文链接:http://blog.csdn.net/qq_38070608/article/details/78469410