国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
Java 中的模式範(fàn)例
範(fàn)例1:使用數(shù)字列印金字塔的一半。
範(fàn)例2:列印數(shù)字箭頭。
範(fàn)例3:使用星號(hào)(*)印出完整的金字塔。
範(fàn)例 4:使用數(shù)字列印半倒金字塔。
範(fàn)例 5:使用字母列印半個(gè)金字塔。
例6:字母的印刷圖案。
範(fàn)例 7:使用星號(hào) (*) 列印正方形。
Example 8: Printing rectangle using stars (*).
Example 9: Printing a Diamond using stars.
Example 10: Printing binary numbers in a stair format.
Example 11: Program to print repeating alphabet patterns.
Conclusion
首頁 Java java教程 Java 中的模式

Java 中的模式

Aug 30, 2024 pm 04:24 PM
java

在 Java 中的模式一文中,在學(xué)習(xí) Java 中的任何程式語言並深入研究高階概念之前,了解循環(huán)的工作原理非常重要。雖然有 3 種類型的循環(huán),分別是 for、while 和 do-while 循環(huán)。每個(gè)循環(huán)根據(jù)程式的具體情況使用,因?yàn)樗鼈儽舜寺杂胁煌?。為了使用各種循環(huán)需要一些程式邏輯,為此目的,為程式設(shè)計(jì)師提供了模式練習(xí),因?yàn)樗婕斑壿嫼屯评砟芰Φ氖褂?。例如,它可以在控制臺(tái)螢?zāi)簧狭杏缀螆D形(如三角形、正方形等)、金字塔、各種星形圖案的盒子、數(shù)字和字元樣式。循環(huán)的格式或基本語法可能因一種程式語言而異,但列印這些模式的一般邏輯保持不變。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業(yè)化 | 78 課程系列 | 15 次模擬測驗(yàn)

Java 中的模式範(fàn)例

讓我們透過一些範(fàn)例來了解如何用Java繪製圖案

範(fàn)例1:使用數(shù)字列印金字塔的一半。

代碼:

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();
}
}
}

輸出:

Java 中的模式

在上面的範(fàn)例中,只需要 2 個(gè)基本迴圈即可列印圖案;第一個(gè) for 迴圈用於計(jì)算行數(shù)。在我們的例子中,我們定義了行,即 5 行,否則我們也可以從使用者那裡獲取輸入並將其儲(chǔ)存在變數(shù)中。內(nèi)循環(huán)是列印特定行中的數(shù)字;完成 1 行或「j」循環(huán)結(jié)束後,使用 println() 變更該行。

範(fàn)例2:列印數(shù)字箭頭。

代碼:

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();
}
}
}

輸出:

Java 中的模式

?

在上面的範(fàn)例中,我們需要將箭頭分成兩半,並為每一半使用 2 個(gè)循環(huán)。行的前半部將是為行設(shè)定的初始值,而下半部的行計(jì)數(shù)比初始值少 1。兩半的內(nèi)循環(huán)用於根據(jù)外循環(huán)迭代每一行。

範(fàn)例3:使用星號(hào)(*)印出完整的金字塔。

代碼:

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();
}
}
}

輸出:

Java 中的模式

在上面的範(fàn)例中,我們需要做 3 件事,也就是記住第一個(gè) for 迴圈從 1 到 rows 變數(shù)印出金字塔的總行數(shù)。其次,我們首先需要列印金字塔中的空格,然後列印空格後面的圖案(*)。對於第二個(gè)和第三個(gè),for迴圈在外循環(huán)「i」內(nèi)使用。

範(fàn)例 4:使用數(shù)字列印半倒金字塔。

代碼:

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();
}
}
}

輸出:

Java 中的模式

簡單的半金字塔很容易,因?yàn)槲覀冃枰幚頂?shù)字、*或我們正在打印的字符,但對於反向金字塔,我們需要先打印空格,然後打印模式,在我們的例子中是 (*) 。因此使用了 3 個(gè) for 循環(huán),其工作原理與完整金字塔的情況類似。

範(fàn)例 5:使用字母列印半個(gè)金字塔。

代碼:

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();
}
}
}

輸出:

Java 中的模式

金字塔的列印邏輯與上面範(fàn)例中使用的邏輯相同,使用 2 個(gè) for 循環(huán),一個(gè)用於行數(shù),其他用於特定行中的字元列印。但主要要注意的是字元資料的處理。例如Java中‘A’的數(shù)值是65,所以所有的數(shù)學(xué)邏輯都是用字母的數(shù)值進(jìn)行的,最後以字元格式列印出來。

例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();
}
}
}

輸出:

Java 中的模式

上面範(fàn)例中處理字元值和 2 個(gè) for 迴圈所遵循的基本模式與範(fàn)例 5 類似,唯一的差異是用於列印所需模式的簡單邏輯。

範(fàn)例 7:使用星號(hào) (*) 列印正方形。

代碼:

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();
}
}
}

輸出:

Java 中的模式

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:

Java 中的模式

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:

Java 中的模式

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:

Java 中的模式

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:

Java 中的模式

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 中的模式的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

現(xiàn)代爪哇的異步編程技術(shù) 現(xiàn)代爪哇的異步編程技術(shù) Jul 07, 2025 am 02:24 AM

Java支持異步編程的方式包括使用CompletableFuture、響應(yīng)式流(如ProjectReactor)以及Java19 中的虛擬線程。 1.CompletableFuture通過鍊式調(diào)用提升代碼可讀性和維護(hù)性,支持任務(wù)編排和異常處理;2.ProjectReactor提供Mono和Flux類型實(shí)現(xiàn)響應(yīng)式編程,具備背壓機(jī)制和豐富的操作符;3.虛擬線程減少並發(fā)成本,適用於I/O密集型任務(wù),與傳統(tǒng)平臺(tái)線程相比更輕量且易於擴(kuò)展。每種方式均有適用場景,應(yīng)根據(jù)需求選擇合適工具並避免混合模型以保持簡潔性

了解Java Nio及其優(yōu)勢 了解Java Nio及其優(yōu)勢 Jul 08, 2025 am 02:55 AM

JavaNIO是Java1.4引入的新型IOAPI,1)面向緩衝區(qū)和通道,2)包含Buffer、Channel和Selector核心組件,3)支持非阻塞模式,4)相比傳統(tǒng)IO更高效處理並發(fā)連接。其優(yōu)勢體現(xiàn)在:1)非阻塞IO減少線程開銷,2)Buffer提升數(shù)據(jù)傳輸效率,3)Selector實(shí)現(xiàn)多路復(fù)用,4)內(nèi)存映射加快文件讀寫。使用時(shí)需注意:1)Buffer的flip/clear操作易混淆,2)非阻塞下需手動(dòng)處理不完整數(shù)據(jù),3)Selector註冊需及時(shí)取消,4)NIO並非適用於所有場景。

在Java中使用枚舉的最佳實(shí)踐 在Java中使用枚舉的最佳實(shí)踐 Jul 07, 2025 am 02:35 AM

在Java中,枚舉(enum)適合表示固定常量集合,最佳實(shí)踐包括:1.用enum表示固定狀態(tài)或選項(xiàng),提升類型安全和可讀性;2.為枚舉添加屬性和方法以增強(qiáng)靈活性,如定義字段、構(gòu)造函數(shù)、輔助方法等;3.使用EnumMap和EnumSet提高性能和類型安全性,因其基於數(shù)組實(shí)現(xiàn)更高效;4.避免濫用enum,如動(dòng)態(tài)值、頻繁變更或複雜邏輯場景應(yīng)使用其他方式替代。正確使用enum能提升代碼質(zhì)量並減少錯(cuò)誤,但需注意其適用邊界。

什麼是匿名的內(nèi)部班級(jí)? 什麼是匿名的內(nèi)部班級(jí)? Jul 07, 2025 am 02:18 AM

匿名內(nèi)部類在Java中用於即時(shí)創(chuàng)建子類或?qū)崿F(xiàn)接口,常用於覆蓋方法以實(shí)現(xiàn)特定目的,如GUI應(yīng)用中的事件處理。其語法形式為new接口或類後直接定義類體,並要求訪問的局部變量必須是final或等效不可變的。它們雖便捷但不宜過度使用,尤其在邏輯複雜時(shí),可用Java8 的Lambda表達(dá)式替代。

Java中的單例設(shè)計(jì)模式是什麼? Java中的單例設(shè)計(jì)模式是什麼? Jul 09, 2025 am 01:32 AM

單例設(shè)計(jì)模式在Java中通過私有構(gòu)造器和靜態(tài)方法確保一個(gè)類只有一個(gè)實(shí)例並提供全局訪問點(diǎn),適用於控制共享資源的訪問。實(shí)現(xiàn)方式包括:1.懶加載,即首次請求時(shí)才創(chuàng)建實(shí)例,適用於資源消耗大且不一定需要的情況;2.線程安全處理,通過同步方法或雙重檢查鎖定確保多線程環(huán)境下只創(chuàng)建一個(gè)實(shí)例,並減少性能影響;3.餓漢式加載,在類加載時(shí)直接初始化實(shí)例,適合輕量級(jí)對像或可接受提前初始化的場景;4.枚舉實(shí)現(xiàn),利用Java枚舉天然支持序列化、線程安全及防止反射攻擊的特性,是推薦的簡潔可靠方式。不同實(shí)現(xiàn)方式可根據(jù)具體需求選

Java字符串與StringBuilder vs StringBuffer Java字符串與StringBuilder vs StringBuffer Jul 09, 2025 am 01:02 AM

String不可變,StringBuilder可變且非線程安全,StringBuffer可變且線程安全。 1.String一旦創(chuàng)建內(nèi)容不可修改,適合少量拼接;2.StringBuilder適合單線程頻繁拼接,性能高;3.StringBuffer適合多線程共享場景,但性能略低;4.合理設(shè)置初始容量、避免循環(huán)中用String拼接能提升性能。

mysql結(jié)合功能 mysql結(jié)合功能 Jul 09, 2025 am 01:09 AM

COALESCE函數(shù)用於返回參數(shù)列表中第一個(gè)非空值,適用於處理NULL數(shù)據(jù)。 1.基本用法是替換NULL值,例如用默認(rèn)聯(lián)繫方式替代空字段;2.可用於聚合查詢中設(shè)置默認(rèn)值,確保無數(shù)據(jù)時(shí)返回0而非NULL;3.可與其他函數(shù)如NULLIF、IFNULL配合使用,增強(qiáng)數(shù)據(jù)清洗和邏輯判斷能力。

註釋處理在Java中的工作方式 註釋處理在Java中的工作方式 Jul 08, 2025 am 02:50 AM

註解處理器是Java編譯階段的一種擴(kuò)展機(jī)制,用於掃描和處理源碼中的註解,並可生成新代碼或進(jìn)行預(yù)處理。其核心作用包括:1.定義註解時(shí)需指定保留策略與目標(biāo)元素類型;2.實(shí)現(xiàn)AbstractProcessor類並重寫關(guān)鍵方法如getSupportedAnnotationTypes、getSupportedSourceVersion和process;3.註冊處理器通過在META-INF/services目錄下配置文件聲明全限定名。註解處理器廣泛應(yīng)用於框架中,如Dagger、ButterKnife和Roo

See all articles