Java打印星號圖案和數(shù)字圖案的示例代碼
使用循環(huán)和控制語句打印圖案
在 Java 中,使用循環(huán)和控制語句是打印圖案的最佳方法。循環(huán)可以幫助你重復(fù)執(zhí)行一段代碼,直到滿足某個(gè)條件為止;而控制語句則允許你在滿足某些條件時(shí)改變程序的流程。下面是一些常見的 Java 圖案程序示例。
1. 星號圖案
星號圖案是 Java 中非常流行的圖案程序,常用于創(chuàng)建有趣的視覺設(shè)計(jì)或圖形。這些程序使用星號(*)或其他符號來創(chuàng)建各種形狀和圖案。星號圖案通常用于計(jì)算機(jī)圖形、標(biāo)志設(shè)計(jì)和其他視覺展示。
創(chuàng)建星號圖案涉及使用嵌套循環(huán)來控制行數(shù)、列數(shù)以及星號的位置。程序可以定制化,以創(chuàng)建包括三角形、正方形、圓形等在內(nèi)的各種圖案。以下是一些常見的星號圖案示例:
1.1 星號金字塔
public class StarPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// 打印空格
for (int j = rows - i; j > 0; j--) {
System.out.print(" ");
}
// 打印星號
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
}
}
輸出:
*
***
*****
*******
*********
1.2 右三角形
public class RightTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
輸出:
*
* *
* * *
* * * *
* * * * *
1.3 左三角形
public class LeftTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// 打印空格
for (int j = rows - i; j > 0; j--) {
System.out.print(" ");
}
// 打印星號
for (int k = 1; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
輸出:
*
* *
* * *
* * * *
* * * * *
1.4 菱形圖案
public class DiamondPattern {
public static void main(String[] args) {
int rows = 5;
// 上半部分
for (int i = 1; i <= rows; i++) {
for (int j = rows - i; j > 0; j--) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
// 下半部分
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows - i; j > 0; j--) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
}
}
輸出:
*
***
*****
*******
*********
*******
*****
***
*
2. 數(shù)字圖案
數(shù)字圖案是另一種常見的 Java 圖案程序,涉及按特定序列或排列打印數(shù)字。這些程序可以用于創(chuàng)建表格、圖表等視覺展示。
創(chuàng)建數(shù)字圖案涉及使用循環(huán)來控制行數(shù)、列數(shù)以及打印的數(shù)字值。程序可以定制化,以創(chuàng)建包括乘法表、斐波那契數(shù)列等在內(nèi)的各種圖案。以下是一些常見的數(shù)字圖案示例:
2.1 數(shù)字金字塔
public class NumberPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// 打印空格
for (int j = rows - i; j > 0; j--) {
System.out.print(" ");
}
// 打印數(shù)字
for (int k = 1; k <= i; k++) {
System.out.print(k + " ");
}
System.out.println();
}
}
}
輸出:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2.2 乘法表
public class MultiplicationTable {
public static void main(String[] args) {
int rows = 10;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows; j++) {
System.out.printf("%d * %d = %d\t", i, j, i * j);
}
System.out.println();
}
}
}
輸出:
1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 1 * 10 = 10
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63 7 * 10 = 70
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 80
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90
10 * 1 = 10 10 * 2 = 20 10 * 3 = 30 10 * 4 = 40 10 * 5 = 50 10 * 6 = 60 10 * 7 = 70 10 * 8 = 80 10 * 9 = 90 10 * 10 = 100
2.3 斐波那契數(shù)列
public class FibonacciPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
int a = 0, b = 1;
for (int j = 1; j <= i; j++) {
System.out.print(a + " ");
int sum = a + b;
a = b;
b = sum;
}
System.out.println();
}
}
}
輸出:
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
小結(jié)
通過上述示例,你可以看到在 Java 中打印圖案的基本方法。使用嵌套循環(huán)和適當(dāng)?shù)目刂普Z句,可以輕松地生成各種復(fù)雜的圖案。這些練習(xí)不僅有助于理解循環(huán)和控制結(jié)構(gòu),還可以提升你的編程技能。希望這些示例對你有所幫助!
以上就是Java打印星號圖案和數(shù)字圖案的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java打印星號和數(shù)字圖案的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Mybatis-plus多數(shù)據(jù)源配置的兩種方式總結(jié)
這篇文章主要為大家詳細(xì)介紹了Mybatis-plus中多數(shù)據(jù)源配置的兩種方式,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起了解一下2022-10-10
Java 自定義Spring框架與Spring IoC相關(guān)接口分析
Spring框架是由于軟件開發(fā)的復(fù)雜性而創(chuàng)建的。Spring使用的是基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅僅限于服務(wù)器端的開發(fā)2021-10-10
idea激活A(yù)ctivateJrebel熱部署的方法詳解
這篇文章主要介紹了idea激活A(yù)ctivateJrebel熱部署的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot整合Thymeleaf小項(xiàng)目及詳細(xì)流程
這篇文章主要介紹了SpringBoot整合Thymeleaf小項(xiàng)目,本項(xiàng)目使用SpringBoot開發(fā),jdbc5.1.48,主要涉及到Mybatis的使用,Thymeleaf的使用,用戶密碼加密,驗(yàn)證碼的設(shè)計(jì),圖片的文件上傳(本文件上傳到本地,沒有傳到數(shù)據(jù)庫)登錄過濾,需要的朋友可以參考下2022-03-03
maven項(xiàng)目連接MySQL方式(使用原生的jdbc)
文章介紹了如何在項(xiàng)目中添加依賴、組織目錄結(jié)構(gòu)、配置數(shù)據(jù)庫連接屬性以及編寫工具類,并分享了個(gè)人經(jīng)驗(yàn),鼓勵(lì)讀者參考和支持腳本之家2026-03-03
SpringBoot_Cache自定義使用SimpleCacheManager方式
這篇文章主要介紹了SpringBoot_Cache自定義使用SimpleCacheManager方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot集成XXL-JOB實(shí)現(xiàn)任務(wù)管理全流程
XXL-JOB 是一款輕量級分布式任務(wù)調(diào)度平臺,功能豐富、界面簡潔、易于擴(kuò)展,本文介紹如何通過 Spring Boot 項(xiàng)目,使用 RestTemplate 和 Feign 的方式調(diào)用 XXL-JOB 后臺管理接口,實(shí)現(xiàn)任務(wù)的全生命周期管理,需要的朋友可以參考下2025-08-08

