Java 打印字母 R 图案的程序

在这个 Java 图案程序中,我们展示了使用 for 循环、while 循环和函数打印字母 R 形状或图案星号的步骤。

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 ((i == 0 || i == rows / 2) && (0 < j && j < rows - 1))
{
System.out.printf("* ");
}
else if ((i != 0) && (j == 0 || (j == rows - 1 && i < rows / 2 )))
{
System.out.printf("* ");
}
else if(j == i && i >= rows / 2)
{
System.out.printf("* ");
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
}
}
Enter Rows = 14
  * * * * * * * * * * * *     
*                         *   
*                         *   
*                         *   
*                         *   
*                         *   
*                         *   
* * * * * * * * * * * * *     
*               *             
*                 *           
*                   *         
*                     *       
*                       *     
*                         *   

在这个字母 R 形状或图案 程序 中,我们用 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 ((i == 0) && (0 < j && j < rows - 1))
{
System.out.printf("* ");
}
else if (i != 0 && j == 0)
{
System.out.printf("* ");
}
else if (i == rows / 2 && j != rows - 1)
{
System.out.printf("* ");
}
else if (i != 0 && j == rows - 1 && i < rows / 2)
{
System.out.printf("* ");
}
else if (j == i && i >= rows / 2)
{
System.out.printf("* ");
}
else
{
System.out.printf(" ");
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Rows = 15
  * * * * * * * * * * * * *   
*                           * 
*                           * 
*                           * 
*                           * 
*                           * 
*                           * 
* * * * * * * * * * * * * *   
*               *             
*                 *           
*                   *         
*                     *       
*                       *     
*                         *   
*                           * 

在此程序中,我们创建了一个 AlphabetRShape 函数来打印用星号填充的字母 R 图案或形状(在行和列上)。

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);

AlphabetRShape(rows, a);
}

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