在 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 <p><strong>輸出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500625821960.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>在上面的範例中,只需要 2 個基本迴圈即可列印圖案;第一個 for 迴圈用於計算行數。在我們的例子中,我們定義了行,即 5 行,否則我們也可以從使用者那裡獲取輸入並將其儲存在變數中。內循環是列印特定行中的數字;完成 1 行或「j」循環結束後,使用 println() 變更該行。 </p> <h4 id="範例-列印數字箭頭">範例2:列印數字箭頭。 </h4> <p><strong>代碼:</strong></p> <pre class="brush:php;toolbar:false">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=1; i--) { //innermost loop is to print the numbers in the specific rows //for the lower half of arrow for (j=1; j <p><strong>輸出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626089284.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p> </p> <p>在上面的範例中,我們需要將箭頭分成兩半,並為每一半使用 2 個循環。行的前半部將是為行設定的初始值,而下半部的行計數比初始值少 1。兩半的內循環用於根據外循環迭代每一行。 </p> <h4 id="範例-使用星號-印出完整的金字塔">範例3:使用星號(*)印出完整的金字塔。 </h4> <p><strong>代碼:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>輸出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626263747.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>在上面的範例中,我們需要做 3 件事,也就是記住第一個 for 迴圈從 1 到 rows 變數印出金字塔的總行數。其次,我們首先需要列印金字塔中的空格,然後列印空格後面的圖案(*)。對於第二個和第三個,for迴圈在外循環「i」內使用。 </p> <h4 id="範例-使用數字列印半倒金字塔">範例 4:使用數字列印半倒金字塔。 </h4> <p><strong>代碼:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>輸出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626323687.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>簡單的半金字塔很容易,因為我們需要處理數字、*或我們正在打印的字符,但對於反向金字塔,我們需要先打印空格,然後打印模式,在我們的例子中是 (*) 。因此使用了 3 個 for 循環,其工作原理與完整金字塔的情況類似。 </p> <h4 id="範例-使用字母列印半個金字塔">範例 5:使用字母列印半個金字塔。 </h4> <p><strong>代碼:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>輸出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626563367.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>金字塔的列印邏輯與上面範例中使用的邏輯相同,使用 2 個 for 循環,一個用於行數,其他用於特定行中的字元列印。但主要要注意的是字元資料的處理。例如Java中‘A’的數值是65,所以所有的數學邏輯都是用字母的數值進行的,最後以字元格式列印出來。 </p> <h4 id="例-字母的印刷圖案">例6:字母的印刷圖案。 </h4> <p><strong>代碼:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>輸出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626797696.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>上面範例中處理字元值和 2 個 for 迴圈所遵循的基本模式與範例 5 類似,唯一的差異是用於列印所需模式的簡單邏輯。 </p> <h4 id="範例-使用星號-列印正方形">範例 7:使用星號 (*) 列印正方形。 </h4> <p><strong>代碼:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>輸出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500627317712.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>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.</p> <h4 id="Example-Printing-rectangle-using-stars">Example 8: Printing rectangle using stars (*).</h4> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500627438442.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>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.</p> <h4 id="Example-Printing-a-Diamond-using-stars">Example 9: Printing a Diamond using stars.</h4> <p>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.</p> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">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; i0; i--) { //innermost loop for the space present in the inverted pyramid for (j=1; j <p>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.</p> <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500627714497.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <h4 id="Example-Printing-binary-numbers-in-a-stair-format">Example 10: Printing binary numbers in a stair format.</h4> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500627989535.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>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.</p> <h4 id="Example-Program-to-print-repeating-alphabet-patterns">Example 11: Program to print repeating alphabet patterns.</h4> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">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=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中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

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

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境