Java 中的模式
在 Java 中的模式一文中,在學習 Java 中的任何程式語言並深入研究高階概念之前,了解循環的工作原理非常重要。雖然有 3 種類型的循環,分別是 for、while 和 do-while 循環。每個循環根據程式的具體情況使用,因為它們彼此略有不同。為了使用各種循環需要一些程式邏輯,為此目的,為程式設計師提供了模式練習,因為它涉及邏輯和推理能力的使用。例如,它可以在控制台螢幕上列印幾何圖形(如三角形、正方形等)、金字塔、各種星形圖案的盒子、數字和字元樣式。循環的格式或基本語法可能因一種程式語言而異,但列印這些模式的一般邏輯保持不變。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗Java 中的模式範例
讓我們透過一些範例來了解如何用Java繪製圖案
範例1:使用數字列印金字塔的一半。
代碼:
public class Pyramid { public static void main(String[] args) { int i, j; //outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++) { //innermost loop is to print the numbers in the specific rows for (j=1; j<=i; j++) { System.out.print(j +" " ); } System.out.println(); } } }
輸出:
在上面的範例中,只需要 2 個基本迴圈即可列印圖案;第一個 for 迴圈用於計算行數。在我們的例子中,我們定義了行,即 5 行,否則我們也可以從使用者那裡獲取輸入並將其儲存在變數中。內循環是列印特定行中的數字;完成 1 行或「j」循環結束後,使用 println() 變更該行。
範例2:列印數字箭頭。
代碼:
public class NumberTriangle { public static void main(String[] args) { int i, j; int rows =7; //outermost loop to represent the number of rows which is 7 in this case //for the upper half of arrow for (i=1; i<= rows; i++) { //innermost loop is to print the numbers in the specific rows //for the upper half of arrow for (j=1; j<=i; j++) { System.out.print(j + " "); } System.out.println(); } //outermost loop to represent the number of rows which is 6 in this case //for the lower half of arrow for (i=rows-1; i>=1; i--) { //innermost loop is to print the numbers in the specific rows //for the lower half of arrow for (j=1; j<=i; j++) { System.out.print(j + " "); } System.out.println(); } } }
輸出:
在上面的範例中,我們需要將箭頭分成兩半,並為每一半使用 2 個循環。行的前半部將是為行設定的初始值,而下半部的行計數比初始值少 1。兩半的內循環用於根據外循環迭代每一行。
範例3:使用星號(*)印出完整的金字塔。
代碼:
public class FullPyramid { public static void main(String[] args) { int i, j, k; int rows = 5; //outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= rows; i++) { //innermost loop to represent the spaces in pyramid for (j= 1; j<= rows-i; j++) { System.out.print(" "); } //innermost loop to represent the stars (*) in pyramid for (k= 1; k<= 2*i-1; k++) { System.out.print("* "); } System.out.println(); } } }
輸出:
在上面的範例中,我們需要做 3 件事,也就是記住第一個 for 迴圈從 1 到 rows 變數印出金字塔的總行數。其次,我們首先需要列印金字塔中的空格,然後列印空格後面的圖案(*)。對於第二個和第三個,for迴圈在外循環「i」內使用。
範例 4:使用數字列印半倒金字塔。
代碼:
public class ReversePyramid { public static void main(String[] args) { int i, j, k; int rows = 5; //outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= rows; i++) { //innermost loop to represent the spaces for (j= 1; j<= rows-1; j++) { System.out.print(" "); } //innermost loop to represent the stars (*) in pyramid for (k= 1; k<= i; k++) { System.out.print("* "); } System.out.println(); } } }
輸出:
簡單的半金字塔很容易,因為我們需要處理數字、*或我們正在打印的字符,但對於反向金字塔,我們需要先打印空格,然後打印模式,在我們的例子中是 (*) 。因此使用了 3 個 for 循環,其工作原理與完整金字塔的情況類似。
範例 5:使用字母列印半個金字塔。
代碼:
public class AlphabetPyramid { public static void main(String[] args) { int i, j; //outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++) { int ch = 65; //innermost loop to represent the alphabets in a pyramid in particular row for (j= 1; j<= i; j++) { System.out.print((char)(ch + i - 1) + " "); } System.out.println(); } } }
輸出:
金字塔的列印邏輯與上面範例中使用的邏輯相同,使用 2 個 for 循環,一個用於行數,其他用於特定行中的字元列印。但主要要注意的是字元資料的處理。例如Java中‘A’的數值是65,所以所有的數學邏輯都是用字母的數值進行的,最後以字元格式列印出來。
例6:字母的印刷圖案。
代碼:
public class AlphabetPattern { public static void main(String[] args) { int i, j; //outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++) { int ch = 65; //innermost loop to represent the alphabets for (j= 1; j<= i; j++) { System.out.print((char)(ch - 1 + j) + " "); } System.out.println(); } } }
輸出:
上面範例中處理字元值和 2 個 for 迴圈所遵循的基本模式與範例 5 類似,唯一的差異是用於列印所需模式的簡單邏輯。
範例 7:使用星號 (*) 列印正方形。
代碼:
public class SquarePattern { public static void main(String[] args) { int i, j; //outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++) { int ch = 65; //innermost loop to represent the stars (*) for (j= 1; j<= 5; j++) { System.out.print(" * " + " "); } System.out.println(); } } }
輸出:
For printing of square, we need length and width, i.e. both sides of the square should be the same, which is 5 in our case. So the first loop is used for the length or number of rows in the square, and the inner loop is used for the width of the square, i.e. 5 stars in a single row.
Example 8: Printing rectangle using stars (*).
Code:
public class RectanglePattern { public static void main(String[] args) { int i, j; //outermost loop to represent the number of rows which is 5 in this case for(i= 1; i<= 5; i++) { int ch = 65; //innermost loop to represent columns the stars (*) for (j= 1; j<= 9; j++) { System.out.print(" * " + " " ); } System.out.println(); } } }
Output:
The basic logic of printing the rectangle of (*) is the same as printing of squares, the only difference between is the different length and width of the rectangle. Here ‘i’ loop is for the length of the rectangle, and the inner ‘j’ loop is for the width of the loop. Our program is taken as a constant value; we can also ask the user and store them in separate variables.
Example 9: Printing a Diamond using stars.
Printing a diamond in Java is a very simple process. It involves printing 2 pyramids, 1 in the upward direction and another in an inverted direction. Basically, we need to use the loops to do the coding to print two separate pyramids.
Code:
public class Diamond { public static void main(String[] args) { int i, j, k; int rows = 5; //outermost loop to represent the number of rows which is 5 in this case. // Creating upper pyramid for(i= 1; i<= rows; i++) { //innermost loop to represent the spaces in upper pyramid for (j= 1; j<= rows-i; j++) { System.out.print(" "); } //innermost loop to represent the stars (*) in upper pyramid for (k= 1; k<= 2*i-1; k++) { System.out.print("* "); } System.out.println(); } //outermost loop for the rows in the inverted pyramid for (i = rows-1; i>0; i--) { //innermost loop for the space present in the inverted pyramid for (j=1; j<= rows - i; j++) { System.out.print(" "); } //innermost loop inside the outer loop to print the ( * ) pattern in inverted pyramid for (k = 1; k<= 2*i-1; k++) { System.out.print("* "); } System.out.println(); } } }
In the above example, almost the same logic is applied to create both pyramids, one in an upward direction and another in an inverted direction. Thus, the first loop is for the number of lines or rows in the pattern, and the second is for spaces and the stars (*) pattern in the pattern.
Output:
Example 10: Printing binary numbers in a stair format.
Code:
public class BinaryStair { public static void main(String[] args) { int i, j; //outer loop for the total rows which is 5 in this case for (i = 1; i <= 5; i++) { //inner loop for the pattern of 0 and 1 in each row for (j = 1; j<= i ; j++) { if (j % 2 ==0) { System.out.print(0); } else { System.out.print(1); } } System.out.println(); } } }
Output:
In the above example, in order to print binary pattern, outer for loop ‘i’ is used for a total number of rows, and the inner for loop ‘j’ is used to iterate till the outer loop ‘i’ because for the 1st row, we need 1 value, for the 2nd row we need 2 values, and so on. If and else statements are used in order to print the alternate value of 0 and 1. Suppose for the first time i=1, j=1 and 1%2 != 0, then 1 is printed, and execution will move out of the inner loop.
Example 11: Program to print repeating alphabet patterns.
Code:
public class AlphabetReverseOrder { public static void main(String[] args) { int i, j, k; //outer loop for the total rows which is 5 in this case for (i = 0 ; i<=5; i++) { int ch= 65; //inner loop for the pattern of alphabets in till ‘i’ loop for (j = 0; j <=i ; j++) { System.out.print((char) (ch+j) + " "); } //inner loop for the pattern of alphabets in reverse order from ‘i’ loop for (k= i-1; k >=0; k--) { System.out.print((char) (ch+k) + " "); } System.out.println(); } } }
Output:
In the above example, if we observe each row of pattern, we need to print the alphabet first in the increasing order, i.e. A B and then in the reverse order, i.e. A B A. For this, we need 3 loops, 1st for loop for the total number of rows. 2nd for loop to print the alphabets in increasing order then the 3rd for loop which remains inside the outer ‘i’ loop and prints the alphabets in the same line but in reverse order of ‘j’ loop.
Conclusion
The above example and their explanations clearly show how to make such patterns in Java. Though these patterns seem to be difficult in the starting, observing them deeply of how the repetition of pattern is happening in a single row and according to how many loops should be used, it becomes easy to do hands-on on this. Today also, in interviews of big companies, candidates are asked to write the logic of patterns of varying difficulty levels because this pattern making shows the basic logical and programming knowledge of an individual.
以上是Java 中的模式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。
