编写一个Java程序,使用for循环打印空心右侧帕斯卡星形三角形。
package ShapePrograms2;
import java.util.Scanner;
public class HollowRightPascal1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int i, j, k;
System.out.print("Enter Hollow Right Pascals Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Right Pascals Triangle Star Pattern");
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j <= i; j++ )
{
if(j == 1 || j == i) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
for (i = 1; i <= rows - 1; i++ )
{
for (j = rows - 1; j >= i; j-- )
{
if(j == rows - 1 || j == i || i == rows) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
for(k = 1; k < i; k++)
{
System.out.print(" ");
}
System.out.println();
}
}
}

这个 Java示例 使用while循环显示空心右侧帕斯卡三角形图案中的星形。
package ShapePrograms2;
import java.util.Scanner;
public class HollowRightPascal2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Hollow Right Pascals Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Right Pascals Triangle Star Pattern");
int i = 1, j, k;
while ( i <= rows )
{
j = 1 ;
while ( j <= i )
{
if(j == 1 || j == i) {
System.out.print("*");
}
else {
System.out.print(" ");
}
j++;
}
System.out.println();
i++;
}
i = 1;
while ( i <= rows - 1 )
{
j = rows - 1;
while ( j >= i )
{
if(j == rows - 1 || j == i || i == rows) {
System.out.print("*");
}
else {
System.out.print(" ");
}
j--;
}
k = 1;
while( k < i)
{
System.out.print(" ");
k++;
}
System.out.println();
i++;
}
}
}
Enter Hollow Right Pascals Triangle Pattern Rows = 8
Printing Hollow Right Pascals Triangle Star Pattern
*
**
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**
*
使用do-while循环打印空心右侧帕斯卡星形三角形的Java程序。
package ShapePrograms2;
import java.util.Scanner;
public class HollowRightPascal3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Hollow Right Pascals Triangle Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Hollow Right Pascals Triangle Star Pattern");
int i = 1, j, k;
do
{
j = 1 ;
do
{
if(j == 1 || j == i) {
System.out.print("*");
}
else {
System.out.print(" ");
}
} while ( ++j <= i );
System.out.println();
} while ( ++i <= rows );
i = 1;
do
{
j = rows - 1;
do
{
if(j == rows - 1 || j == i || i == rows) {
System.out.print("*");
}
else {
System.out.print(" ");
}
} while ( --j >= i ) ;
k = 1;
do
{
System.out.print(" ");
} while( ++k < i);
System.out.println();
} while ( ++i <= rows - 1 ) ;
}
}
Enter Hollow Right Pascals Triangle Pattern Rows = 11
Printing Hollow Right Pascals Triangle Star Pattern
*
**
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
**
*