Java 程序:打印空心直角三角形星形图案
原文:https://www.studytonight.com/java-programs/java-program-to-print-hollow-right-triangle-star-pattern
在本教程中,我们将看到如何在 Java 中打印空心直角三角形星形图案。首先,我们将要求用户初始化行数。然后,我们将使用不同的循环来打印空心的直角三角形星形图案。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。
输入:输入行数:4
输出:模式为:
*
- *
- *
程序 1:打印空心直角三角形星形图案
在这个程序中,我们将看到如何使用 for 循环在 java 中打印空心直角三角形星形图案。
算法:
- 开始
- 创建 Scanner 类的实例。
- 声明变量来存储行数。
- 要求用户初始化变量。
- 使用 for 循环打印图案。
- 使用(int I = 1;I < = n;i++)
- 外部 for 循环从 i=1 迭代到 n。
- 如果“If”条件为真,则第一个内部循环打印字符,直到 j<=i
- 如果“If”条件为 false,则第二个内部循环打印 j=1 或 j=i 的字符,直到 j<=i,代表 j!=1 和 j!=n 打印空间。
- 显示结果。
- 停下来。
下面的例子说明了上述算法的实现。
//Java Program To Print the Hollow Right Triangle Star Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
System.out.println("Enter the symbol: ");
char ch = sc.next().charAt(0);
for(int i=1;i<=n;i++)
{
if(i==1 || i==n)
for(int j=1;j<=i;j++)
{
System.out.print(ch);
}
else
{
for(int j=1;j<=i;j++)
{
if(j==1 || j==i)
System.out.print(ch);
else
System.out.print(" ");
}
}
System.out.println();
}
}
}
输入行数:7 输入符号:
- *
- *
- *
- *
- *
程序 2:打印空心直角三角形星形图案
在这个程序中,我们将看到如何使用 while 循环在 java 中打印空心直角三角形星形图案。
算法:
- 开始
- 创建 Scanner 类的实例。
- 声明变量来存储行数。
- 要求用户初始化变量。
- 使用 while 循环打印图案。
- 如果给定的“I”值为真,检查外侧的状况。
- 然后检查“如果”条件。如果为真,则将 j 初始化为 1,检查内部 while 条件 j<=i,如果为真则打印字符并增加 j 值,重复直到条件 j<=i 为假。
- 如果为假,则将 j 初始化为 1,检查 while 条件,条件为真则打印 j=1 或 j=i 的字符,否则打印 j 的空格!=i 和 j!=1.
- 重复,直到外侧的条件为假。
- 显示结果。
- 停下来。
下面的例子说明了上述算法的实现。
//Java Program To Print the Hollow Right Triangle Star Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
int i=1;
int j;
while(i<=n)
{
if(i==1 || i==n)
{
j=1;
while(j <=i)
{
System.out.print("*");
j++;
}
}
else
{
j=1;
while(j<=i)
{
if(j==1 || j==i)
System.out.print("*");
else
System.out.print(" ");
j++;
}
}
System.out.println();
i++;
}
}
}
输入行数:7 *
- *
- *
- *
- *
- *
程序 3:打印空心直角三角形星形图案
在这个程序中,我们将看到如何使用 do-while 循环在 java 中打印空心直角三角形星形图案。
算法:
- 开始
- 创建 Scanner 类的实例。
- 声明变量来存储行数。
- 要求用户初始化变量。
- 使用边做边循环打印图案。
- 检查 if 条件。
- 如果为真,则第一个内部 do 循环打印一个字符,然后在(++j <=i)时检查条件,如果此条件为真,则打印字符,重复直到条件为假。
- 如果为 false,则第二个内部循环打印 j=1 或 j=i 的字符,打印 j=1、j=i 值以外的空格。然后在(++j<=i)时检查条件;,重复直到条件为假。
- 在每次迭代中增加循环变量。
- 重复直到(I < = n);是假的。
- 显示结果。
- 停下来。
下面的例子说明了上述算法的实现。
//Java Program To Print the Hollow Right Triangle Star Pattern
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n=sc.nextInt();
int i=1;
int j;
do
{
if(i==1 || i==n)
{
j=1;
do
{
System.out.print("*");
}while(++j <=i);
}
else
{
j=1;
do
{
if(j==1 || j==i)
System.out.print("*");
else
System.out.print(" ");
}
while(++j<=i);
}
System.out.println();
++i;
} while(i<=n);
}
}
输入行数:8 *
- *
- *
- *
- *
- *
- *