用于显示模式的Java程序
时间:2020-01-09 10:35:34 来源:igfitidea点击:
在本文中,我们将提供用于显示模式的Java程序,这些程序是用于理解和使用Java循环的初学者级程序。在这些Java程序中,外部和内部for循环用于显示使用数字或者符号的模式。
金字塔模式的Java程序–模式1
一个非常流行的金字塔图案是显示数字金字塔,其中数字重复的次数与每一行中的数字相同。
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
for(int i=1; i<=rows; i++){
// print correct number of spaces
// in each row
for(int j=0; j<rows-i; j++){
System.out.print(" ");
}
// print number
for(int k=0; k<i; k++){
System.out.print(i + " ");
}
System.out.println();
}
}
}
倒金字塔的Java程序–模式2
如果要以倒置方式显示金字塔。
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
for(int i=rows; i>=1; i--){
// print correct number of spaces
// in each row
for(int j=0; j<rows-i; j++){
System.out.print(" ");
}
// print number
for(int k=0; k<i; k++){
System.out.print(i + " ");
}
System.out.println();
}
}
}
半金字塔的Java程序–模式3
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
for(int i=1; i<=rows; i++){
for(int j=0; j<i; j++){
System.out.print(i + " ");
}
System.out.println();
}
}
}
半金字塔的Java程序–模式4
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
for(int i=1; i<=rows; i++){
// print correct number of spaces
// in each row
for(int j=0; j<2*(rows-i); j++){
System.out.print(" ");
}
for(int j=1; j<=i; j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
半金字塔的Java程序–模式5
在这种模式下,数字是串联的,而不是在每行中都将其重置。这种图案称为弗洛伊德三角形。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
int num = 1;
for(int i=1; i<=rows; i++){
for(int j=0; j<i; j++){
System.out.print(num++ + " ");
}
System.out.println();
}
}
}
金字塔模式的Java程序–模式6
在同一行中数字递增和递减的金字塔。
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
for(int i=1; i<=rows; i++){
// print correct number of spaces
// in each row
for(int j=0; j<rows-i; j++){
System.out.print(" ");
}
// print incrementing part
for(int k=1; k<i; k++){
System.out.print(k);
}
// print decrementing part
for(int k=i; k>=1; k--){
System.out.print(l);
}
System.out.println();
}
}
}
金字塔模式的Java程序–模式7
金字塔使用" *"符号。
*
* *
* * *
* * * *
* * * * *
* * * * * *
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
for(int i=1; i<=rows; i++){
// print correct number of spaces
// in each row
for(int j=0; j<rows-i; j++){
System.out.print(" ");
}
// print number
for(int k=0; k<i; k++){
System.out.print("* ");
}
System.out.println();
}
}
}
模式的Java程序–模式8
8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 8 7 6 5 4 3 8 7 6 5 4 8 7 6 5 8 7 6 8 7 8
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
for(int i=1; i<=rows; i++){
for(int j=rows; j>=i; j--){
System.out.print(j + " ");
}
System.out.println();
}
}
}
Java模式程序–模式9
1 12 123 1234 12345 123456 1234567 12345678 1234567 123456 12345 1234 123 12 1
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
//For upper half-incrementing
for(int i=1; i<=rows; i++){
for(int j=1; j<=i; j++){
System.out.print(j);
}
System.out.println();
}
//For lower half-decrementing
for(int i=rows; i>=1; i--){
for(int j=1; j<i; j++){
System.out.print(j);
}
System.out.println();
}
}
}
Java模式程序–模式10
12345678 1234567 123456 12345 1234 123 12 1 12 123 1234 12345 123456 1234567 12345678
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
}
private static void displayPyramidPattern(int rows){
for(int i=rows; i>=1; i--){
for(int j=1; j<=i; j++){
System.out.print(j);
}
System.out.println();
}
for(int i=2; i<=rows; i++){
for(int j=1; j<=i; j++){
System.out.print(j);
}
System.out.println();
}
}
}
Java模式程序–模式11
7777777
666666
55555
4444
333
22
1
22
333
4444
55555
666666
7777777
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
sc.close();
}
private static void displayPyramidPattern(int rows){
for(int i=rows; i>=1; i--){
for(int j=i; j<rows; j++){
System.out.print(" ");
}
for(int j = 1; j <= i; j++){
System.out.print(i);
}
System.out.println();
}
for(int i=2; i<=rows; i++){
for(int j=rows; j>i; j--){
System.out.print(" ");
}
for(int j=1; j<=i; j++){
System.out.print(i);
}
System.out.println();
}
}
}
Java模式程序–模式12
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
sc.close();
}
private static void displayPyramidPattern(int rows){
//for upper pyramid
for(int i=rows; i>=1; i--){
// print correct number of spaces
// in each row
for(int j=0; j<rows-i; j++){
System.out.print(" ");
}
// print number
for(int k=0; k<i; k++){
System.out.print(i + " ");
}
System.out.println();
}
//for lower pyramid
for(int i=2; i<=rows; i++){
// print correct number of spaces
// in each row
for(int j=0; j<rows-i; j++){
System.out.print(" ");
}
// print number
for(int k=0; k<i; k++){
System.out.print(i + " ");
}
System.out.println();
}
}
}
Java模式程序–模式13
12345654321
123454321
1234321
12321
121
1
121
12321
1234321
123454321
12345654321
public class PatternProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows in the pyramid (1-9)- ");
int rows = sc.nextInt();
displayPyramidPattern(rows);
sc.close();
}
private static void displayPyramidPattern(int rows){
for(int i=rows; i>=1; i--){
// print correct number of spaces
// in each row
for(int j=0; j<rows-i; j++){
System.out.print(" ");
}
// print incrementing part
for(int k=1; k<i; k++){
System.out.print(k);
}
// print decrementing part
for(int k=i; k>=1; k--){
System.out.print(k);
}
System.out.println();
}
for(int i=2; i<=rows; i++){
// print correct number of spaces
// in each row
for(int j=0; j<rows-i; j++){
System.out.print(" ");
}
// print incrementing part
for(int k=1; k<i; k++){
System.out.print(k);
}
// print decrementing part
for(int k=i; k>=1; k--){
System.out.print(k);
}
System.out.println();
}
}
}

