用java打印的一些小图形
1、等腰三角形
import java. util. Scanner;
public class test1 {
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
System. out. println ( "请输入一个三角形的高" ) ;
int num = input. nextInt ( ) ;
for ( int i = 1 ; i <= num; i++ ) {
for ( int j = 0 ; j < num - i; j++ )
System. out. print ( " " ) ;
for ( int j = 0 ; j < 2 * i- 1 ; j++ )
System. out. print ( "*" ) ;
System. out. println ( ) ;
}
System. out. println ( "\n\n\n\n" ) ;
}
}
2、被挖空的等腰三角形
import java. util. Scanner;
public class test1 {
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
System. out. println ( "请输入一个三角形的高" ) ;
int num = input. nextInt ( ) ;
for ( int i = 1 ; i <= num; i++ ) {
for ( int j = 0 ; j < num - i; j++ )
System. out. print ( " " ) ;
for ( int j = 0 ; j < 2 * i- 1 ; j++ )
{
if ( i == num)
{
if ( j % 2 == 0 )
System. out. print ( "*" ) ;
else
System. out. print ( " " ) ;
} else {
if ( j == 0 || j == ( 2 * i - 2 ) )
System. out. print ( "*" ) ;
else
System. out. print ( " " ) ;
}
}
System. out. println ( ) ;
}
}
}
3、菱形
void t_print ( ) {
Scanner input = new Scanner ( System. in) ;
System. out. println ( "请输入菱形的高" ) ;
int num = input. nextInt ( ) / 2 ;
for ( int i = 1 ; i <= num; i++ ) {
for ( int k = 0 ; k < num- i+ 1 ; k++ )
System. out. print ( " " ) ;
for ( int j = 0 ; j < ( 2 * i - 1 ) ; j++ )
System. out. print ( "*" ) ;
System. out. println ( ) ;
}
for ( int i = num- 1 ; i> 0 ; i-- ) {
for ( int k = 0 ; k < num- i+ 1 ; k++ )
System. out. print ( " " ) ;
for ( int j = 0 ; j < 2 * i- 1 ; j++ )
System. out. print ( "*" ) ;
System. out. println ( ) ;
}
}
4、被挖空的菱形
void t_print ( ) {
Scanner input = new Scanner ( System. in) ;
System. out. println ( "请输入菱形的高" ) ;
int num = input. nextInt ( ) / 2 ;
for ( int i = 1 ; i <= num; i++ ) {
for ( int k = 0 ; k < num- i+ 1 ; k++ )
System. out. print ( " " ) ;
for ( int j = 0 ; j < ( 2 * i - 1 ) ; j++ )
{
if ( j == 0 || j == 2 * i- 2 )
System. out. print ( "*" ) ;
else
System. out. print ( " " ) ;
}
System. out. println ( ) ;
}
for ( int i = num- 1 ; i> 0 ; i-- ) {
for ( int k = 0 ; k < num- i+ 1 ; k++ )
System. out. print ( " " ) ;
for ( int j = 0 ; j < 2 * i- 1 ; j++ ) {
if ( j == 0 || j == 2 * i- 2 )
System. out. print ( "*" ) ;
else
System. out. print ( " " ) ;
}
System. out. println ( ) ;
}
}
打印杨辉三角
import java. util. Scanner;
public class Test {
public static void main ( String[ ] args) {
Scanner input = new Scanner ( System. in) ;
System. out. print ( "请输入杨辉三角的高:" ) ;
int num = input. nextInt ( ) ;
int [ ] [ ] arr = new int [ num] [ ] ;
arr[ 0 ] = new int [ 1 ] ;
arr[ 0 ] [ 0 ] = 1 ;
for ( int i = 1 ; i < num; i++ ) {
arr[ i] = new int [ i+ 1 ] ;
for ( int j = 0 ; j <= i; j++ ) {
if ( j == i || j == 0 ) {
arr[ i] [ j] = 1 ;
continue ;
}
arr[ i] [ j] = arr[ i- 1 ] [ j- 1 ] + arr[ i- 1 ] [ j] ;
}
}
for ( int i = 0 ; i < arr. length; i++ ) {
for ( int j = 0 ; j < arr[ i] . length; j++ ) {
System. out. print ( arr[ i] [ j] + "\t" ) ;
}
System. out. println ( ) ;
}
}
}