Java打印菱形星图案程序
在本文中,我们将学习如何使用Java打印菱形星图案。该图案是通过使用多个for循环和打印语句形成的。
输出
以下是菱形星图案的演示:
The diamond star pattern :
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*
不同的方法
以下是打印菱形星图案的不同方法:
使用用户自定义输入
以下是打印菱形星图案的步骤:
- 导入Scanner类。
- 声明变量i、j、k和my_input。
- 创建一个Scanner对象来读取用户输入。
- 提示用户输入图案大小的数字。
- 将k设置为my_input - 1。
- 使用嵌套for循环打印菱形上半部分的空格和星号。
- 每行之后打印一个新行。
- 调整k并使用嵌套for循环打印菱形的下半部分。
- 结束程序。
示例
这里,输入是根据提示由用户输入的:
import java.util.Scanner;
public class DiamondStar{
public static void main(String args[]){
int i, j, k, my_input;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.print("Enter the number : ");
my_input = my_scanner.nextInt();
k = my_input - 1;
System.out.println("The diamond star pattern : ");
for (j = 1; j<= my_input; j++){
for (i = 1; i<= k; i++){
System.out.print(" ");
}
k--;
for (i = 1; i <= 2 * j - 1; i++){
System.out.print("*");
}
System.out.println("");
}
k = 1;
for (j = 1; j<= my_input - 1; j++){
for (i = 1; i<= k; i++){
System.out.print(" ");
}
k++;
for (i = 1; i<= 2 * (my_input - j) - 1; i++){
System.out.print("*");
}
System.out.println("");
}
}
}
输出
Required packages have been imported
A reader object has been defined
Enter the number : 8
The diamond star pattern :
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*
使用递归
以下是使用递归打印菱形星图案的步骤:
- 首先,我们将导入Scanner类。
- 提示用户输入菱形上半部分的行数 (n)。
- 打印菱形上半部分我们将使用递归方法 (printUpperDiamond),对于基本条件,当currentRow超过n时我们将停止。
- 要打印空格,我们将使用一个辅助函数来打印n - currentRow个空格,要打印星号,我们将使用另一个辅助函数来打印2 * currentRow - 1个星号。
- 我们将再次调用该方法,使用 currentRow + 1 来处理下一行。
- 要打印菱形下半部分我们将使用递归方法 (printLowerDiamond),对于基本条件,当n变为0时我们将停止。
- 要打印空格,我们将使用一个辅助函数来打印currentRow个空格,要打印星号,我们将使用另一个辅助函数来打印2 * n - 1个星号。
- 我们将再次调用该方法,使用n - 1和currentRow + 1来处理下一行。
- 辅助函数打印空格,而printStars将递归地打印指定数量的星号。
示例
这里,整数之前已定义,其值将在控制台上访问和显示。
import java.util.Scanner;
public class DiamondStar {
public static void main(String[] args) {
// Scanner to take input from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows for the diamond: ");
int n = scanner.nextInt();
// Print the upper part of the diamond
printUpperDiamond(n, 1);
// Print the lower part of the diamond
printLowerDiamond(n - 1, 1);
}
// Method to print the upper part of the diamond
static void printUpperDiamond(int n, int currentRow) {
if (currentRow > n) {
return;
}
printSpaces(n - currentRow);
printStars(2 * currentRow - 1);
System.out.println();
printUpperDiamond(n, currentRow + 1);
}
// Method to print the lower part of the diamond
static void printLowerDiamond(int n, int currentRow) {
if (n == 0) {
return;
}
printSpaces(currentRow);
printStars(2 * n - 1);
System.out.println();
printLowerDiamond(n - 1, currentRow + 1);
}
// Helper method to print spaces
static void printSpaces(int count) {
if (count == 0) {
return;
}
System.out.print(" ");
printSpaces(count - 1);
}
// Helper method to print stars
static void printStars(int count) {
if (count == 0) {
return;
}
System.out.print("*");
printStars(count - 1);
}
}
输出
The number of rows is defined as 8
The diamond star pattern :
*
***
*****
*******
*********
***********
*************
***************
*************
***********
*********
*******
*****
***
*
上述程序的时间和空间复杂度为:
时间复杂度:O(n²)
空间复杂度:O(n)
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP