Java 中的模式
在 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<= 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(); } } }
输出:
在上面的示例中,只需要 2 个基本循环即可打印图案;第一个 for 循环用于计算行数。在我们的例子中,我们定义了行,即 5 行,否则我们也可以从用户那里获取输入并将其存储在变量中。内循环是打印特定行中的数字;完成 1 行或“j”循环结束后,使用 println() 更改该行。
示例2:打印数字箭头。
代码:
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(); } } }
输出:
在上面的示例中,我们需要将箭头分成两半,并为每一半使用 2 个循环。行的前半部分将是为行设置的初始值,而下半部分的行计数比初始值少 1。两半的内循环用于根据外循环迭代每一行。
示例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(); } } }
输出:
在上面的示例中,我们需要做 3 件事,即记住第一个 for 循环从 1 到 rows 变量打印金字塔的总行数。其次,我们首先需要打印金字塔中的空格,然后打印空格后面的图案(*)。对于第二个和第三个,在外循环“i”内部使用了 for 循环。
示例 4:使用数字打印半倒金字塔。
代码:
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(); } } }
输出:
简单的半金字塔很容易,因为我们需要处理数字、*或我们正在打印的字符,但对于反向金字塔,我们需要首先打印空格,然后打印模式,在我们的例子中是 (*) 。因此使用了 3 个 for 循环,其工作原理与完整金字塔情况下的循环类似。
示例 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(); } } }
输出:
金字塔的打印逻辑与上面示例中使用的逻辑相同,使用 2 个 for 循环,一个用于行数,其他用于特定行中的字符打印。但主要需要注意的是字符数据的处理。比如Java中‘A’的数值是65,所以所有的数学逻辑都是用字母的数值进行的,最后以字符格式打印出来。
例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(); } } }
输出:
上面示例中处理字符值和 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(); } } }
输出:
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:
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:
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:
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:
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
