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

目錄
Java 中的模式範例
範例1:使用數(shù)字列印金字塔的一半。
範例2:列印數(shù)字箭頭。
範例3:使用星號(*)印出完整的金字塔。
範例 4:使用數(shù)字列印半倒金字塔。
範例 5:使用字母列印半個金字塔。
例6:字母的印刷圖案。
範例 7:使用星號 (*) 列印正方形。
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 中的模式一文中,在學習 Java 中的任何程式語言並深入研究高階概念之前,了解循環(huán)的工作原理非常重要。雖然有 3 種類型的循環(huán),分別是 for、while 和 do-while 循環(huán)。每個循環(huán)根據(jù)程式的具體情況使用,因為它們彼此略有不同。為了使用各種循環(huán)需要一些程式邏輯,為此目的,為程式設(shè)計師提供了模式練習,因為它涉及邏輯和推理能力的使用。例如,它可以在控制臺螢?zāi)簧狭杏缀螆D形(如三角形、正方形等)、金字塔、各種星形圖案的盒子、數(shù)字和字元樣式。循環(huán)的格式或基本語法可能因一種程式語言而異,但列印這些模式的一般邏輯保持不變。

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

Java 中的模式範例

讓我們透過一些範例來了解如何用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 個基本迴圈即可列印圖案;第一個 for 迴圈用於計算行數(shù)。在我們的例子中,我們定義了行,即 5 行,否則我們也可以從使用者那裡獲取輸入並將其儲存在變數(shù)中。內(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 個循環(huán)。行的前半部將是為行設(shè)定的初始值,而下半部的行計數(shù)比初始值少 1。兩半的內(nèi)循環(huán)用於根據(jù)外循環(huán)迭代每一行。

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

輸出:

Java 中的模式

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

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

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

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

輸出:

Java 中的模式

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

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

輸出:

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

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

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

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

Dreamweaver CS6

Dreamweaver CS6

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

SublimeText3 Mac版

SublimeText3 Mac版

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

選擇特定的列|性能優(yōu)化 選擇特定的列|性能優(yōu)化 Jun 27, 2025 pm 05:46 PM

1.FetchingAllColumnSIncreaseSemory,網(wǎng)絡(luò)和ProPersingSingoverHead.2.unnectaryDatareTrievalPreventSefefectivefectivefective.2.nynynyneedcolumnsimprovesperformenceByReDucingReSouranceByReDucingRessourceUsage.1.fetchingallcolumnsincreasemory

Java中的'枚舉”類型是什麼? Java中的'枚舉”類型是什麼? Jul 02, 2025 am 01:31 AM

Java中的枚舉(enum)是一種特殊的類,用於表示固定數(shù)量的常量值。 1.使用enum關(guān)鍵字定義;2.每個枚舉值都是該枚舉類型的公共靜態(tài)最終實例;3.可以包含字段、構(gòu)造函數(shù)和方法,為每個常量添加行為;4.可在switch語句中使用,支持直接比較,並提供name()、ordinal()、values()和valueOf()等內(nèi)置方法;5.枚舉可提升代碼的類型安全性、可讀性和靈活性,適用於狀態(tài)碼、顏色或星期等有限集合場景。

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

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

什麼是JDK? 什麼是JDK? Jun 25, 2025 pm 04:05 PM

JDK(JavaDevelopmentKit)是用於開發(fā)Java應(yīng)用程序和小程序的軟件開發(fā)環(huán)境,包含編譯、調(diào)試和運行Java程序所需的工具與庫。其核心組件包括Java編譯器(javac)、Java運行時環(huán)境(JRE)、Java解釋器(java)、調(diào)試器(jdb)、文檔生成工具(javadoc)及打包工具(如jar和jmod)。開發(fā)者需要JDK來編寫、編譯Java代碼,並藉助IDE進行開發(fā);沒有JDK則無法構(gòu)建或修改Java應(yīng)用??赏ㄟ^在終端輸入javac-version和java-version

Java設(shè)置指南的VSCODE調(diào)試器 Java設(shè)置指南的VSCODE調(diào)試器 Jul 01, 2025 am 12:22 AM

配置Java調(diào)試環(huán)境在VSCode上的關(guān)鍵步驟包括:1.安裝JDK並驗證;2.安裝JavaExtensionPack和DebuggerforJava插件;3.創(chuàng)建並配置launch.json文件,指定mainClass和projectName;4.設(shè)置正確的項目結(jié)構(gòu),確保源碼路徑和編譯輸出正確;5.使用調(diào)試技巧如Watch、F8/F10/F11快捷鍵及處理常見問題如類找不到或JVM附加失敗的方法。

XML規(guī)則:避免的常見錯誤 XML規(guī)則:避免的常見錯誤 Jun 22, 2025 am 12:09 AM

避免XML錯誤的方法包括:1.確保元素正確嵌套,2.轉(zhuǎn)義特殊字符。正確嵌套避免解析錯誤,而轉(zhuǎn)義字符防止文檔損壞,使用XML編輯器可幫助維護結(jié)構(gòu)完整性。

Windows搜索欄未輸入 Windows搜索欄未輸入 Jul 02, 2025 am 10:55 AM

Windows搜索欄無法輸入文字時,常見的解決方法有:1.重啟資源管理器或電腦,可打開任務(wù)管理器重新啟動“Windows資源管理器”進程,或直接重啟設(shè)備;2.切換或卸載輸入法,嘗試使用英文輸入法或微軟自帶輸入法,排除第三方輸入法衝突;3.運行系統(tǒng)文件檢查工具,在命令提示符中執(zhí)行sfc/scannow命令修復系統(tǒng)文件;4.重置或重建搜索索引,通過“控制面板”中的“索引選項”進行重建。通常先從簡單步驟開始排查,多數(shù)問題可以逐步解決。

如何為Java開發(fā)設(shè)置VS代碼? 如何為Java開發(fā)設(shè)置VS代碼? Jun 29, 2025 am 12:23 AM

要使用VSCode進行Java開發(fā),需安裝必要擴展、配置JDK和設(shè)置工作區(qū)。 1.安裝JavaExtensionPack,包含語言支持、調(diào)試集成、構(gòu)建工具和代碼補全功能;可選裝JavaTestRunner或SpringBoot擴展包。 2.安裝至少JDK17,並通過java-version和javac-version驗證;設(shè)置JAVA_HOME環(huán)境變量,或在VSCode底部狀態(tài)欄切換多個JDK。 3.打開項目文件夾後,確保項目結(jié)構(gòu)正確並啟用自動保存,調(diào)整格式化規(guī)則、啟用代碼檢查,並配置編譯任務(wù)以優(yōu)化開

See all articles