Java打印字母W图案程序

在这个Java图案程序中,我们展示了如何使用for循环、while循环和函数来打印字母W形状的星形图案。

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Rows = ");
int rows = sc.nextInt();

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if(j == 0 || j == rows - 1)
{
System.out.printf("* ");
}
else if ((i + j == rows - 1 || i == j) && (i >= rows / 2))
{
System.out.printf("* ");
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Enter Rows = 15
*                           * 
*                           * 
*                           * 
*                           * 
*                           * 
*                           * 
*                           * 
*             *             * 
*           *   *           * 
*         *       *         * 
*       *           *       * 
*     *               *     * 
*   *                   *   * 
* *                       * * 
*                           * 

在这个字母W形状或图案程序中,我们用while循环替换了for循环来在每个位置打印星形。有关更多字母图案星形图案程序,请使用超链接。

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);
int rows, i, j;

System.out.print("Enter Rows = ");
rows = sc.nextInt();

i = 0 ;
while ( i < rows )
{
j = 0;
while ( j < rows)
{
if(j == 0 || j == rows - 1)
{
System.out.printf("* ");
}
else if ((i + j == rows - 1 || i == j) && (i >= rows / 2))
{
System.out.printf("* ");
}
else
{
System.out.printf(" ");
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Rows = 17
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*               *               * 
*             *   *             * 
*           *       *           * 
*         *           *         * 
*       *               *       * 
*     *                   *     * 
*   *                       *   * 
* *                           * * 
*                               * 

在这个程序中,我们创建了一个AlphabetWShape函数来打印由行和列中的星形填充的字母W图案或形状。

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Rows = ");
int rows = sc.nextInt();

System.out.print("Enter Character = ");
char a = sc.next().charAt(0);

AlphabetWShape(rows, a);
}

public static void AlphabetWShape(int rows, char a)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if(j == 0 || j == rows - 1)
{
System.out.printf("%c ", a);
}
else if ((i + j == rows - 1 || i == j) && (i >= rows / 2))
{
System.out.printf("%c ", a);
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Print Alphabet W Pattern