打印倒星形图案的 Java 程序
在本文中,我们将了解如何打印倒星图案。该模式是通过使用多个 for 循环和 print 语句形成的。
以下是相同的演示 -
输入
假设我们的输入是 -
输入尺寸: 8
输出
所需的输出将是 -
The inverted star pattern***************
*************
***********
*********
*******
*****
***
*
算法
Step 1 - STARTStep 2 - Declare four integer values namely i, j, k and my_input.
Step 3 - Read the required values from the user/ define the values
Step 4 - We iterate through two nested 'for' loops to get space between the characters.
Step 5 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help print the required character.
Step 6 - Now, print a newline to get the specific number of characters in the subsequent lines.
Step 7 - Display the result
Step 8 - Stop
示例 1
这里,输入是由用户根据提示输入的。中试用此示例 。
import java.util.Scanner;输出结果public class InvertedStar{
public static void main(String[] args){
int my_input, k, i, j;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("已定义阅读器对象 ");
System.out.print("输入尺寸: ");
my_input = my_scanner.nextInt(); System.out.println("The inverted star pattern");
for ( i = my_input ; i >= 1; i-- ){
for ( j = 0 ; j < my_input - i; j++ ){
System.out.print(" ");
}
for ( k = 0 ; k != (2 * i) - 1; k++ ){
System.out.print("*");
}
System.out.println();
}
}
}
Required packages have been imported已定义阅读器对象
输入尺寸: 8
The inverted star pattern
***************
*************
***********
*********
*******
*****
***
*
示例 2
在这里,整数已经被预先定义,它的值被访问并显示在控制台上。
public class InvertedStar{输出结果public static void main(String[] args){
int my_input, k, i, j;
char my_character;
my_input = 8;
System.out.println("大小定义为 " +my_input);
System.out.println("The inverted star pattern");
for ( i = my_input ; i >= 1; i-- ){
for ( j = 0 ; j < my_input - i; j++ ){
System.out.print(" ");
}
for ( k = 0 ; k != (2 * i) - 1; k++ ){
System.out.print("*");
}
System.out.println();
}
}
}
大小定义为 8The inverted star pattern
***************
*************
***********
*********
*******
*****
***
*
以上是 打印倒星形图案的 Java 程序 的全部内容, 来源链接: utcz.com/z/297182.html