编写一个Java程序,使用for循环打印左箭头星形图案。在这个Java左箭头的例子中,第一个嵌套的for循环集打印左箭头的顶部部分,而另一个代码则打印左箭头的底部。
package ShapePrograms;
import java.util.Scanner;
public class LeftArrow1 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
int i, j;
System.out.print("Enter Left Arrow Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Left Arrow Star Pattern");
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j <= rows - i; j++ )
{
System.out.print(" ");
}
for (j = i ; j <= rows; j++ )
{
System.out.print("\*");
}
System.out.println();
}
for (i = 1 ; i < rows; i++ )
{
for (j = 0 ; j < i; j++ )
{
System.out.print(" ");
}
for (j = 0 ; j <= i; j++ )
{
System.out.print("\*");
}
System.out.println();
}
}
在这个Java左箭头星形图案程序中,我们用一个while循环代替了for循环。
package ShapePrograms;
import java.util.Scanner;
public class LeftArrow2 { private static Scanner scanner;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter Left Arrow Pattern Rows = ");
int rows = sc.nextInt();
int i = 1, j;
System.out.println("Printing Left Arrow Star Pattern");
while(i <= rows)
{
j = 1 ;
while ( j <= rows - i )
{
System.out.print(" ");
j++;
}
j = i;
while(j <= rows )
{
System.out.print("\*");
j++;
}
System.out.println();
i++;
}
i = 1;
while (i < rows )
{
j = 0 ;
while ( j < i)
{
System.out.print(" ");
j++;
}
j = 0 ;
while (j <= i )
{
System.out.print("\*");
j++;
}
System.out.println();
i++;
}
}
Enter Left Arrow Pattern Rows = 7
Printing Left Arrow Star Pattern
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
使用do while循环打印左箭头星形图案的Java程序
package ShapePrograms;
import java.util.Scanner;
public class LeftArrow3 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter Left Arrow Pattern Rows = ");
int rows = sc.nextInt();
int i = 1, j;
System.out.println("Printing Left Arrow Star Pattern");
do
{
j = 1 ;
do
{
System.out.print(" ");
} while ( j++ <= rows - i ) ;
j = i;
do
{
System.out.print("\*");
} while(++j <= rows ) ;
System.out.println();
} while(++i <= rows) ;
i = 1;
do
{
j = 0 ;
do
{
System.out.print(" ");
} while ( j++ < i) ;
j = 0 ;
do
{
System.out.print("\*");
} while (++j <= i ) ;
System.out.println();
} while (++i < rows );
}
Enter Left Arrow Pattern Rows = 9
Printing Left Arrow Star Pattern
*********
********
*******
******
*****
****
***
**
*
**
***
****
*****
******
*******
********
*********
在这个Java例子中,leftArrowPattern函数打印了一个给定符号的左箭头图案。
package ShapePrograms;
import java.util.Scanner;
public class LeftArrow4 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter Left Arrow Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for Left Arrow Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("Printing Left Arrow Pattern");
leftArrowPattern(rows, ch);
}
public static void leftArrowPattern(int rows, char ch) {
int i, j;
for (i = 1 ; i <= rows; i++ )
{
for (j = 1 ; j <= rows - i; j++ )
{
System.out.print(" ");
}
for (j = i ; j <= rows; j++ )
{
System.out.print( ch);
}
System.out.println();
}
for (i = 1 ; i < rows; i++ )
{
for (j = 0 ; j < i; j++ )
{
System.out.print(" ");
}
for (j = 0 ; j <= i; j++ )
{
System.out.print(ch);
}
System.out.println();
}
}
Enter Left Arrow Pattern Rows = 11
Enter Character for Left Arrow Pattern = $
Printing Left Arrow Pattern
$$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$
$$
$$$
$$$$
$$$$$
$$$$$$
$$$$$$$
$$$$$$$$
$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$$