你今天應該檢查的 12 個 Java 模式程序

已發表: 2020-07-28

準備技術面試很棘手,如果你是 Java 專業人士,事情就更複雜了。 分析 Java 專業人員的專業知識的一種流行方法是查看使用 Java 編寫模式程序的能力。 您可能必須製作一個獨特的 Java 模式程序,這並不普遍用於確定面試。

不用擔心,因為在本文中,我們將了解多種 Java 模式,以便您可以

我們在這裡討論的所有模式都是由數字組成的。 練習這些模式的最佳方法是嘗試創建第一個模式,如果您在某個時候遇到困難,請將您的代碼與我們的代碼進行比較。 這樣,您將了解什麼是做什麼的,並且不會對這些程序產生任何混淆。

我們建議您從第一個模式開始。 如果您有一些創建 Java 模式程序的經驗,您可以從我們在下面分享的任何設計開始:

閱讀:印度的 Java 開發人員薪水

目錄

簡單的三角形圖案

圖案:

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

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

//從用戶那裡獲取行值

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

for (int i = 1; i <= 行; i++)

{

for (int j = 1; j <= i; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//關閉資源

sc.close();

}

}

雙三角形圖案

圖案:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6 7

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

//從用戶那裡獲取行值

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

//打印圖案的上半部分

for (int i = 1; i <= 行; i++)

{

for (int j = 1; j <= i; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//打印圖案的下半部分

for (int i = rows-1; i >= 1; i–)

{

for (int j = 1; j <= i; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//關閉資源

sc.close();

}

}

了解更多:您應該親身體驗的 8 個 Github 上的 Java 項目

具有重複數字的三角形圖案

圖案:

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

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

//從用戶那裡獲取行值

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

for (int i = 1; i <= 行; i++)

{

for (int j = 1; j <= i; j++)

{

System.out.print(i+" ");

}

System.out.println();

}

//關閉資源

sc.close();

}

}

具有下降數字的倒三角形

圖案:

7 6 5 4 3 2 1

7 6 5 4 3 2

7 6 5 4 3

7 6 5 4

7 6 5

7 6

7

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

//從用戶那裡獲取行值

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

for (int i = 1; i <= 行; i++)

{

for (int j = rows; j >= i; j–)

{

System.out.print(j+" ");

}

System.out.println();

}

//關閉資源

sc.close();

}

}

具有重複圖案的三角形

圖案:

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

1 2 3 4 5 6 5 4 3 2 1

1 2 3 4 5 6 7 6 5 4 3 2 1

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

//從用戶那裡獲取行值

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

for (int i = 1; i <= 行; i++)

{

//打印前半行

for (int j = 1; j <= i; j++)

{

System.out.print(j+" ");

}

//打印行的後半部分

for (int j = i-1; j >= 1; j–)

{

System.out.print(j+" ");

}

System.out.println();

}

//關閉資源

sc.close();

}

}

雙三角形圖案

圖案:

1 2 3 4 5 6 7

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

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

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

//從用戶那裡獲取行值

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

//打印圖案的上半部分

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 <= 行; i++)

{

for (int j = 1; j <= i; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//關閉資源

sc.close();

}

}

倒雙三角形

圖案:

1234567

234567

34567

4567

567

67

7

67

567

4567

34567

234567

1234567

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

//從用戶那裡獲取行值

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

//打印圖案的上半部分

for (int i = 1; i <= 行; i++)

{

//在每行的開頭打印i個空格

for (int j = 1; j < i; j++)

{

System.out.print(" ");

}

//將i打印到每行末尾的行值

for (int j = i; j <= 行; j++)

{

System.out.print(j);

}

System.out.println();

}

//打印圖案的下半部分

for (int i = rows-1; i >= 1; i–)

{

//在每行的開頭打印i個空格

for (int j = 1; j < i; j++)

{

System.out.print(" ");

}

//將i打印到每行末尾的行值

for (int j = i; j <= 行; j++)

{

System.out.print(j);

}

System.out.println();

}

//關閉資源

sc.close();

}

}

雙倒三角形

圖案:

1 2 3 4 5 6 7

2 3 4 5 6 7

3 4 5 6 7

4 5 6 7

5 6 7

6 7

7

6 7

5 6 7

4 5 6 7

3 4 5 6 7

2 3 4 5 6 7

1 2 3 4 5 6 7

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

//從用戶那裡獲取行值

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

//打印圖案的上半部分

for (int i = 1; i <= 行; i++)

{

//在每行的開頭打印i個空格

for (int j = 1; j < i; j++)

{

System.out.print(" ");

}

//將i打印到每行末尾的行值

for (int j = i; j <= 行; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//打印圖案的下半部分

for (int i = rows-1; i >= 1; i–)

{

//在每行的開頭打印i個空格

for (int j = 1; j < i; j++)

{

System.out.print(" ");

}

//將i打印到每行末尾的行值

for (int j = i; j <= 行; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//關閉資源

sc.close();

}

}

數字支柱模式

圖案:

1111111

1111122

1111333

1114444

1155555

1666666

7777777

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

for (int i = 1; i <= 行; i++)

{

for (int j = 1; j <= rows-i; j++)

{

System.out.print(1);

}

for (int j = 1; j <= i; j++)

{

System.out.print(i);

}

System.out.println();

}

sc.close();

}

}

二進制數字模式

圖案:

1010101

0101010

1010101

0101010

1010101

0101010

1010101

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

for (int i = 1; i <= 行; i++)

{

整數;

如果(i%2 == 0)

{

數 = 0;

for (int j = 1; j <= 行; j++)

{

System.out.print(num);

數=(數== 0)? 1:0;

}

}

別的

{

數 = 1;

for (int j = 1; j <= 行; j++)

{

System.out.print(num);

數=(數== 0)? 1:0;

}

}

System.out.println();

}

sc.close();

}

}

上升三角形圖案

圖案:

1

2 6

3 7 10

4 8 11 13

5 9 12 14 15

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

for (int i = 1; i <= 行; i++)

{

整數 = 我;

for (int j = 1; j <= i; j++)

{

System.out.print(num+" ");

數=數+行-j;

}

System.out.println();

}

sc.close();

}

}

Square Java 模式程序

圖案:

1 2 3 4 5 6 7

2 3 4 5 6 7 1

3 4 5 6 7 1 2

4 5 6 7 1 2 3

5 6 7 1 2 3 4

6 7 1 2 3 4 5

7 1 2 3 4 5 6

代碼:

導入 java.util.Scanner;

公共類主類

{

公共靜態無效主要(字符串 [] 參數)

{

掃描儀 sc = new Scanner(System.in);

System.out.println(“你想要這個模式有多少行?”);

int 行 = sc.nextInt();

System.out.println(“這是你的模式……!!!”);

for(int i=1;i< 行+1 ;i++)

{

for(int j=i;j < 行+1 ;j++)

{

System.out.print(j + ” “);

}

for(int k=1;k < i ;k++)

{

System.out.print(k + ” “);

}

System.out.println();

}

sc.close();

}

}

另請閱讀: 2020 年的 Python 與 Java:您應該選擇哪一個?

最後的想法

我們確信您已準備好在瀏覽此列表後用 Java 創建模式程序。 您可以根據自己的經驗和專業知識從任何模式開始。 如果您對此主題或本文有任何疑問,請在下面的評論部分告訴我們。

如果您有興趣了解有關 Java、全棧軟件開發的更多信息,請查看 upGrad 和 IIIT-B 的全棧軟件開發 PG 文憑,該文憑專為在職專業人士設計,提供 500 多個小時的嚴格培訓,9 個以上的項目和任務、IIIT-B 校友身份、實用的實踐頂點項目和頂級公司的工作協助。

踏上夢想的工作

升級和 IIIT-BANGALORE 的 PG 文憑在全棧開發中
現在申請