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

目錄
Java 中的模式示例
示例1:使用數(shù)字打印金字塔的一半。
示例2:打印數(shù)字箭頭。
示例3:使用星號(hào)(*)打印完整的金字塔。
示例 4:使用數(shù)字打印半倒金字塔。
示例 5:使用字母打印半個(gè)金字塔。
例6:字母的印刷圖案。
示例 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
首頁(yè) Java java教程 Java 中的模式

Java 中的模式

Aug 30, 2024 pm 04:24 PM
java

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

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

Java 中的模式示例

讓我們通過(guò)一些例子了解如何用Java繪制圖案

示例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 中的模式

在上面的示例中,只需要 2 個(gè)基本循環(huán)即可打印圖案;第一個(gè) for 循環(huán)用于計(jì)算行數(shù)。在我們的例子中,我們定義了行,即 5 行,否則我們也可以從用戶那里獲取輸入并將其存儲(chǔ)在變量中。內(nèi)循環(huán)是打印特定行中的數(shù)字;完成 1 行或“j”循環(huán)結(jié)束后,使用 println() 更改該行。

示例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 中的模式

?

在上面的示例中,我們需要將箭頭分成兩半,并為每一半使用 2 個(gè)循環(huán)。行的前半部分將是為行設(shè)置的初始值,而下半部分的行計(jì)數(shù)比初始值少 1。兩半的內(nèi)循環(huán)用于根據(jù)外循環(huá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 中的模式

在上面的示例中,我們需要做 3 件事,即記住第一個(gè) for 循環(huán)從 1 到 rows 變量打印金字塔的總行數(shù)。其次,我們首先需要打印金字塔中的空格,然后打印空格后面的圖案(*)。對(duì)于第二個(gè)和第三個(gè)?,在外循環(huán)“i”內(nèi)部使用了 for 循環(huá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 中的模式

簡(jiǎn)單的半金字塔很容易,因?yàn)槲覀冃枰幚頂?shù)字、*或我們正在打印的字符,但對(duì)于反向金字塔,我們需要首先打印空格,然后打印模式,在我們的例子中是 (*) 。因此使用了 3 個(gè) for 循環(huán),其工作原理與完整金字塔情況下的循環(huá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 中的模式

金字塔的打印邏輯與上面示例中使用的邏輯相同,使用 2 個(gè) for 循環(huán),一個(gè)用于行數(shù),其他用于特定行中的字符打印。但主要需要注意的是字符數(shù)據(jù)的處理。比如Java中‘A’的數(shù)值是65,所以所有的數(shù)學(xué)邏輯都是用字母的數(shù)值進(jìn)行的,最后以字符格式打印出來(lái)。

例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 中的模式

上面示例中處理字符值和 2 個(gè) for 循環(huán)所遵循的基本模式與示例 5 類似,唯一的區(qū)別是用于打印所需模式的簡(jiǎ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)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系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脫衣機(jī)

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集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

將語(yǔ)義結(jié)構(gòu)應(yīng)用于html的文章,部分和旁邊 將語(yǔ)義結(jié)構(gòu)應(yīng)用于html的文章,部分和旁邊 Jul 05, 2025 am 02:03 AM

在HTML中合理使用語(yǔ)義化標(biāo)簽?zāi)芴嵘?yè)面結(jié)構(gòu)清晰度、可訪問(wèn)性和SEO效果。1.用于獨(dú)立內(nèi)容區(qū)塊,如博客文章或評(píng)論,需保持自包含性;2.用于歸類相關(guān)內(nèi)容,通常包含標(biāo)題,適用于頁(yè)面不同模塊;3.用于與主內(nèi)容相關(guān)但非核心的輔助信息,如側(cè)邊欄推薦或作者簡(jiǎn)介。實(shí)際開(kāi)發(fā)中應(yīng)結(jié)合、等標(biāo)簽,避免過(guò)度嵌套,保持結(jié)構(gòu)簡(jiǎn)潔,并通過(guò)開(kāi)發(fā)者工具驗(yàn)證結(jié)構(gòu)合理性。

請(qǐng)求的操作需要高程窗戶 請(qǐng)求的操作需要高程窗戶 Jul 04, 2025 am 02:58 AM

遇到“此操作需要提升權(quán)限”提示時(shí),說(shuō)明你需要管理員權(quán)限才能繼續(xù)。解決方法包括:1.右鍵選擇“以管理員身份運(yùn)行”程序或設(shè)置快捷方式始終以管理員身份運(yùn)行;2.檢查當(dāng)前賬戶是否為管理員賬戶,若不是則切換或請(qǐng)求管理員協(xié)助;3.用管理員權(quán)限打開(kāi)命令提示符或PowerShell執(zhí)行相關(guān)命令;4.在必要時(shí)通過(guò)獲取文件所有權(quán)或修改注冊(cè)表等手段繞過(guò)限制,但此類操作需謹(jǐn)慎并充分了解風(fēng)險(xiǎn)。確認(rèn)權(quán)限身份并嘗試上述方法通??山鉀Q問(wèn)題。

Java中可呼叫和可運(yùn)行的差異 Java中可呼叫和可運(yùn)行的差異 Jul 04, 2025 am 02:50 AM

Callable和Runnable在Java中主要有三點(diǎn)區(qū)別。第一,Callable的call()方法可以返回結(jié)果,適合需要返回值的任務(wù),如Callable;而Runnable的run()方法無(wú)返回值,適用于無(wú)需返回的任務(wù),如日志記錄。第二,Callable允許拋出checked異常,便于錯(cuò)誤傳遞;而Runnable必須在內(nèi)部處理異常。第三,Runnable可直接傳給Thread或ExecutorService,而Callable只能提交給ExecutorService,并返回Future對(duì)象以

Java Classloader在內(nèi)部如何工作 Java Classloader在內(nèi)部如何工作 Jul 06, 2025 am 02:53 AM

Java的類加載機(jī)制通過(guò)ClassLoader實(shí)現(xiàn),其核心工作流程分為加載、鏈接和初始化三個(gè)階段。加載階段由ClassLoader動(dòng)態(tài)讀取類的字節(jié)碼并創(chuàng)建Class對(duì)象;鏈接包括驗(yàn)證類的正確性、為靜態(tài)變量分配內(nèi)存及解析符號(hào)引用;初始化則執(zhí)行靜態(tài)代碼塊和靜態(tài)變量賦值。類加載采用雙親委派模型,優(yōu)先委托父類加載器查找類,依次嘗試Bootstrap、Extension和ApplicationClassLoader,確保核心類庫(kù)安全且避免重復(fù)加載。開(kāi)發(fā)者可自定義ClassLoader,如URLClassL

探索Java中不同的同步機(jī)制 探索Java中不同的同步機(jī)制 Jul 04, 2025 am 02:53 AM

Javaprovidesmultiplesynchronizationtoolsforthreadsafety.1.synchronizedblocksensuremutualexclusionbylockingmethodsorspecificcodesections.2.ReentrantLockoffersadvancedcontrol,includingtryLockandfairnesspolicies.3.Conditionvariablesallowthreadstowaitfor

有效處理常見(jiàn)的Java例外 有效處理常見(jiàn)的Java例外 Jul 05, 2025 am 02:35 AM

Java異常處理的關(guān)鍵在于區(qū)分checked和unchecked異常并合理使用try-catch、finally及日志記錄。1.checked異常如IOException需強(qiáng)制處理,適用于可預(yù)期的外部問(wèn)題;2.unchecked異常如NullPointerException通常由程序邏輯錯(cuò)誤引起,屬于運(yùn)行時(shí)錯(cuò)誤;3.捕獲異常時(shí)應(yīng)具體明確,避免籠統(tǒng)捕獲Exception;4.推薦使用try-with-resources自動(dòng)關(guān)閉資源,減少手動(dòng)清理代碼;5.異常處理中應(yīng)結(jié)合日志框架記錄詳細(xì)信息,便于后

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

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

Java中'靜態(tài)”關(guān)鍵字的目的是什么? Java中'靜態(tài)”關(guān)鍵字的目的是什么? Jul 05, 2025 am 02:36 AM

靜態(tài)關(guān)鍵字在Java中用于創(chuàng)建屬于類本身的變量和方法,而非類的實(shí)例。1.靜態(tài)變量被所有類的實(shí)例共享,適用于存儲(chǔ)所有對(duì)象共有的數(shù)據(jù),如Student類中的schoolName。2.靜態(tài)方法屬于類,不依賴對(duì)象,常用于工具函數(shù),如Math.sqrt(),且只能訪問(wèn)其他靜態(tài)成員。3.靜態(tài)代碼塊用于在類加載時(shí)執(zhí)行初始化操作,如加載庫(kù)或設(shè)置日志。4.靜態(tài)內(nèi)部類可以獨(dú)立于外部類實(shí)例化,但無(wú)法訪問(wèn)外部類的非靜態(tài)成員。合理使用static能有效管理類級(jí)別的資源和行為。

See all articles