编写一个Java程序,使用for循环打印V星图案。
package Shapes3;
import java.util.Scanner;
public class VStarPattern1 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter V Shape Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing V Shape Star Pattern");
for (int i = 1 ; i <= rows; 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();
}
}
使用while循环打印V型星体图案的Java程序
package Shapes3;
import java.util.Scanner;
public class VStarPattern2 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
int i, j, k, l;
System.out.print("Enter V Shape Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing V Shape Star Pattern");
i = 1 ;
while( i <= rows)
{
j = 1 ;
while( j <= i )
{
System.out.print("\*");
j++;
}
k = 1 ;
while( k <= 2 \* (rows - i) )
{
System.out.print(" ");
k++;
}
l = 1 ;
while( l <= i )
{
System.out.print("\*");
l++;
}
System.out.println();
i++;
}
}
Enter V Shape Star Pattern Rows = 12
Printing V Shape Star Pattern
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
************************
这个Java例子使用do while循环来显示按字母顺序排列的V星图案。
package Shapes3;
import java.util.Scanner;
public class VStarPattern3 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
int i, j, k, l;
System.out.print("Enter V Shape Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing V Shape Star Pattern");
i = 1 ;
do
{
j = 1 ;
do
{
System.out.print("\*");
} while( ++j <= i );
k = 1 ;
while(k <= 2 \* (rows - i))
{
System.out.print(" ");
k++;
}
l = 1 ;
do
{
System.out.print("\*");
} while( ++l <= i ) ;
System.out.println();
} while(++i <= rows);
}
Enter V Shape Star Pattern Rows = 16
Printing V Shape Star Pattern
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
************ ************
************* *************
************** **************
*************** ***************
********************************
在这个Java例子中,VShapePattern函数允许输入任何字符并打印出给定字符的V型。
package Shapes3.X
import java.util.Scanner;
public class VStarPattern4 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter V Shape Star Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for V Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("Printing V Shape Pattern");
VShapePattern(rows, ch);
}
public static void VShapePattern(int rows, char ch)
{
for (int i = 1 ; i <= rows; 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 V Shape Star Pattern Rows = 19
Enter Character for V Pattern = $
Printing V Shape Pattern
$ $
$$ $$
$$$ $$$
$$$$ $$$$
$$$$$ $$$$$
$$$$$$ $$$$$$
$$$$$$$ $$$$$$$
$$$$$$$$ $$$$$$$$
$$$$$$$$$ $$$$$$$$$
$$$$$$$$$$ $$$$$$$$$$
$$$$$$$$$$$ $$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$$$ $$$$$$$$$$$$$$
$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$