编写一个Java程序,使用for循环打印钻石星形图案。钻石图案是一个金字塔和一个倒金字塔的组合。因此,这个Java星形钻石的例子划分了代码来打印正常和倒置的金字塔。
package ShapePrograms;
import java.util.Scanner;
public class DiamondPattern1 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
int i, j;
System.out.print("Enter Diamond Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Diamond Star Pattern");
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j <= rows - i; j++ )
{
System.out.print(" ");
}
for (j = 1 ; j <= i \* 2 - 1; j++ )
{
System.out.print("\*");
}
System.out.println();
}
for (i = rows - 1 ; i > 0; i-- )
{
for (j = 1 ; j <= rows - i; j++ )
{
System.out.print(" ");
}
for (j = 1 ; j <= i \* 2 - 1; j++ )
{
System.out.print("\*");
}
System.out.println();
}
}
在这个Java钻石图案的星星程序中,我们用一个while循环代替了for循环
// 使用while循环打印钻石星形图案的Java程序 包 ShapePrograms;
import java.util.Scanner;
public class DiamondPattern3 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
int i, j, k;
System.out.print("Enter Diamond Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Diamond Star Pattern");
i = 1 ;
while(i <= rows )
{
j = 1 ;
while( j <= rows - i )
{
System.out.print(" ");
j++;
}
k = 1 ;
while ( k <= i \* 2 - 1 )
{
System.out.print("\*");
k++;
}
System.out.println();
i++;
}
i = rows - 1 ;
while( i > 0 )
{
j = 1 ;
while ( j <= rows - i )
{
System.out.print(" ");
j++;
}
k = 1 ;
while( k <= i \* 2 - 1 )
{
System.out.print("\*");
k++;
}
System.out.println();
i--;
}
}
Enter Diamond Pattern Rows = 12
Printing Diamond Star Pattern
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
使用do while循环打印钻石之星图案的Java程序
package ShapePrograms;
import java.util.Scanner;
public class DiamondPattern4 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
int i, j, k;
System.out.print("Enter Diamond Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Diamond Star Pattern");
i = 1 ;
do
{
j = 1 ;
do
{
System.out.print(" ");
} while( j++ <= rows - i ) ;
k = 1 ;
do
{
System.out.print("\*");
} while ( ++k <= i \* 2 - 1 ) ;
System.out.println();
} while(++i <= rows ) ;
i = rows - 1 ;
do
{
j = 1 ;
do
{
System.out.print(" ");
} while (j++ <= rows - i ) ;
k = 1 ;
do
{
System.out.print("\*");
} while( ++k <= i \* 2 - 1 ) ;
System.out.println();
} while( --i > 0 ) ;
}
Enter Diamond Pattern Rows = 15
Printing Diamond Star Pattern
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
***************************
*************************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
在这个Java例子中,diamondPattern函数打印了一个给定符号的钻石图案。
package ShapePrograms;
import java.util.Scanner;
public class DiamondPattern2 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter Diamond Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for Diamond Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("Printing Diamond Pattern");
diamondPattern(rows, ch);
}
public static void diamondPattern(int rows, char ch) {
int i, j, k;
int x = rows - 1;
int y = 1;
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j <= x; j++ )
{
System.out.print(" ");
}
for (k = 1 ; k <= y; k++ )
{
System.out.print(ch);
}
if(x > i)
{
x = x - 1;
y = y + 2;
}
if(x < i)
{
x = x + 1;
y = y - 2;
}
System.out.println();
}
}
Enter Diamond Pattern Rows = 10
Enter Character for Diamond Pattern = #
Printing Diamond Pattern
#
###
#####
#######
#########
#########
#######
#####
###
#