编写一个 Java 程序,使用 for 循环打印倒 V 形星形图案或方形星形图案中的半个菱形。
package Shapes3;
import java.util.Scanner;
public class InvertedVStarPattern1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Inverted V Shape Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Inverted V Shape Star Pattern");
for (int i = rows; i >= 1; i-- )
{
for (int j = 1 ; j <= i; j++ )
{
System.out.print("*");
}
for (int k = 1 ; k <= 2 * (rows - i); k++ )
{
System.out.print(" ");
}
for (int l = 1 ; l <= i; l++ )
{
System.out.print("*");
}
System.out.println();
}
}
}

此 Java 程序使用 while 循环显示倒 V 形星形图案。
package Shapes3;
import java.util.Scanner;
public class InvertedVStarPattern2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Inverted V Shape Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Inverted V Shape Star Pattern");
int i = rows;
while( i >= 1)
{
int j = 1 ;
while( j <= i )
{
System.out.print("*");
j++;
}
int k = 1 ;
while( k <= 2 * (rows - i) )
{
System.out.print(" ");
k++;
}
int l = 1 ;
while( l <= i )
{
System.out.print("*");
l++;
}
System.out.println();
i--;
}
}
}
Enter Inverted V Shape Star Pattern Rows = 13
Printing Inverted V Shape Star Pattern
**************************
************ ************
*********** ***********
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
使用 do while 循环打印倒 V 形星形图案的 Java 程序
package Shapes3;
import java.util.Scanner;
public class InvertedVStarPattern3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Inverted V Shape Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Inverted V Shape Star Pattern");
int i = rows;
do
{
int j = 1 ;
do
{
System.out.print("*");
} while( ++j <= i );
int k = 1 ;
while(k <= 2 * (rows - i))
{
System.out.print(" ");
k++;
}
int l = 1 ;
do
{
System.out.print("*");
} while( ++l <= i ) ;
System.out.println();
} while(--i >= 1);
}
}
Enter Inverted V Shape Star Pattern Rows = 15
Printing Inverted V Shape Star Pattern
******************************
************** **************
************* *************
************ ************
*********** ***********
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
在此 Java 模式 示例 中,InvertedVShapePattern 函数允许输入任何字符,并打印给定字符的倒 V 形。
package Shapes3;
import java.util.Scanner;
public class InvertedVStarPattern4 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Inverted V Shape Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for Inverted V Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("Printing Inverted V Shape Star Pattern");
InvertedVShapePattern(rows, ch);
}
public static void InvertedVShapePattern(int rows, char ch)
{
for (int i = rows; i >= 1; i-- )
{
for (int j = 1 ; j <= i; j++ )
{
System.out.print(ch);
}
for (int k = 1 ; k <= 2 * (rows - i); k++ )
{
System.out.print(" ");
}
for (int l = 1 ; l <= i; l++ )
{
System.out.print(ch);
}
System.out.println();
}
}
}
Enter Inverted V Shape Star Pattern Rows = 17
Enter Character for Inverted V Pattern = #
Printing Inverted V Shape Star Pattern
##################################
################ ################
############### ###############
############## ##############
############# #############
############ ############
########### ###########
########## ##########
######### #########
######## ########
####### #######
###### ######
##### #####
#### ####
### ###
## ##
# #