编写一个 Java 程序使用 for 循环打印沙漏星型图案。
package ShapePrograms2;
import java.util.Scanner;
public class Sandglass1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int i, j, k;
System.out.print("Please Enter Sandglass Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Sandglass Star Pattern");
for (i = 0 ; i <= rows - 1; i++ )
{
for (j = 0 ; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows - 1; k++)
{
System.out.print("* ");
}
System.out.println();
}
for (i = rows - 1; i >= 0; i-- )
{
for (j = 0 ; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows - 1; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

本 Java 示例 使用 while 循环以沙漏状结构或图案显示星号。
package ShapePrograms2;
import java.util.Scanner;
public class Sandglass2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter Sandglass Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Sandglass Star Pattern");
int i = 0, j, k;
while (i <= rows - 1)
{
j = 0 ;
while(j < i )
{
System.out.print(" ");
j++;
}
k = i;
while( k <= rows - 1)
{
System.out.print("* ");
k++;
}
System.out.println();
i++;
}
i = rows - 1;
while ( i >= 0 )
{
j = 0 ;
while ( j < i)
{
System.out.print(" ");
j++;
}
k = i;
while(k <= rows - 1)
{
System.out.print("* ");
k++;
}
System.out.println();
i--;
}
}
}
Please Enter Sandglass Pattern Rows = 8
Printing Sandglass Star Pattern
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
Java 使用 do while 循环打印沙漏星型图案的程序
package ShapePrograms2;
import java.util.Scanner;
public class Sandglass3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter Sandglass Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Sandglass Star Pattern");
int i = 0, j, k;
do
{
j = 0 ;
do
{
System.out.print(" ");
} while(j++ < i );
k = i;
do
{
System.out.print("* ");
} while( ++k <= rows - 1);
System.out.println();
} while (++i <= rows - 1);
i = rows - 1;
do
{
j = 0 ;
do
{
System.out.print(" ");
} while ( j++ < i);
k = i;
do
{
System.out.print("* ");
} while(++k <= rows - 1);
System.out.println();
} while ( --i >= 0 );
}
}
Please Enter Sandglass Pattern Rows = 9
Printing Sandglass Star Pattern
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
在此 Java 示例中,SandglassPattern 函数打印给定符号的沙漏图案。
package ShapePrograms2;
import java.util.Scanner;
public class Sandglass4 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Please Enter Sandglass Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for Sandglass Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("Printing Sandglass Star Pattern");
SandglassPattern(rows, ch);
}
public static void SandglassPattern(int rows, char ch) {
int i, j, k;
for (i = 0 ; i <= rows - 1; i++ )
{
for (j = 0 ; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows - 1; k++)
{
System.out.print(ch + " ");
}
System.out.println();
}
for (i = rows - 1; i >= 0; i-- )
{
for (j = 0 ; j < i; j++ )
{
System.out.print(" ");
}
for(k = i; k <= rows - 1; k++)
{
System.out.print(ch + " ");
}
System.out.println();
}
}
}
Please Enter Sandglass Pattern Rows = 10
Enter Character for Sandglass Pattern = @
Printing Sandglass Star Pattern
@ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @
@ @ @ @ @ @
@ @ @ @ @
@ @ @ @
@ @ @
@ @
@
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
@ @ @ @ @ @
@ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @