Java 打印矩形星形图案中的 X 程序

在这个 Java 模式程序中,我们展示了使用 for 循环、while 循环和函数在矩形星形中打印 X 的步骤。下面的程序接受用户输入的行数,并使用嵌套的 for 循环和 if else 来遍历行和列。接下来,程序将在矩形形状的星形内打印 X 模式。

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

在这个 程序中,我们用 while 循环 替换了 for 循环,以遍历行和列并在矩形星形图案内打印 X 形状。有关更多星形图案程序,请 单击此处

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

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

i = 0 ;
while (i < rows )
{
j = 0 ;
while (j < rows )
{
if (i == j || i + j == rows - 1)
{
if (i + j == rows - 1)
{
System.out.printf("/");
}
else
{
System.out.printf("\\");
}
}
else
{
System.out.printf("%c", a);
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Rows = 16
Enter Character = #
\##############/
#\############/#
##\##########/##
###\########/###
####\######/####
#####\####/#####
######\##/######
#######\/#######
#######/\#######
######/##\######
#####/####\#####
####/######\####
###/########\###
##/##########\##
#/############\#
/##############\

在这个 Java 模式程序中,我们创建了 XinRectangleStars 函数来在矩形星形图案内打印 X 形状。

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

XinRectangleStars(rows);
}

public static void XinRectangleStars(int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if (i == j || i + j == rows - 1)
{
if (i + j == rows - 1)
{
System.out.printf("/");
}
else {
System.out.printf("\\");
}
}
else
{
System.out.printf("*");
}
}
System.out.println();
}
}
}
Print X inside Rectangle Star Pattern