在 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 <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500625821960.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>在上面的示例中,只需要 2 个基本循环即可打印图案;第一个 for 循环用于计算行数。在我们的例子中,我们定义了行,即 5 行,否则我们也可以从用户那里获取输入并将其存储在变量中。内循环是打印特定行中的数字;完成 1 行或“j”循环结束后,使用 println() 更改该行。</p> <h4 id="示例-打印数字箭头">示例2:打印数字箭头。</h4> <p><strong>代码:</strong></p> <pre class="brush:php;toolbar:false">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=1; i--) { //innermost loop is to print the numbers in the specific rows //for the lower half of arrow for (j=1; j <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626089284.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p> </p> <p>在上面的示例中,我们需要将箭头分成两半,并为每一半使用 2 个循环。行的前半部分将是为行设置的初始值,而下半部分的行计数比初始值少 1。两半的内循环用于根据外循环迭代每一行。</p> <h4 id="示例-使用星号-打印完整的金字塔">示例3:使用星号(*)打印完整的金字塔。</h4> <p><strong>代码:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626263747.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>在上面的示例中,我们需要做 3 件事,即记住第一个 for 循环从 1 到 rows 变量打印金字塔的总行数。其次,我们首先需要打印金字塔中的空格,然后打印空格后面的图案(*)。对于第二个和第三个,在外循环“i”内部使用了 for 循环。</p> <h4 id="示例-使用数字打印半倒金字塔">示例 4:使用数字打印半倒金字塔。</h4> <p><strong>代码:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626323687.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>简单的半金字塔很容易,因为我们需要处理数字、*或我们正在打印的字符,但对于反向金字塔,我们需要首先打印空格,然后打印模式,在我们的例子中是 (*) 。因此使用了 3 个 for 循环,其工作原理与完整金字塔情况下的循环类似。</p> <h4 id="示例-使用字母打印半个金字塔">示例 5:使用字母打印半个金字塔。</h4> <p><strong>代码:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626563367.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>金字塔的打印逻辑与上面示例中使用的逻辑相同,使用 2 个 for 循环,一个用于行数,其他用于特定行中的字符打印。但主要需要注意的是字符数据的处理。比如Java中‘A’的数值是65,所以所有的数学逻辑都是用字母的数值进行的,最后以字符格式打印出来。</p> <h4 id="例-字母的印刷图案">例6:字母的印刷图案。</h4> <p><strong>代码:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500626797696.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>上面示例中处理字符值和 2 个 for 循环所遵循的基本模式与示例 5 类似,唯一的区别是用于打印所需模式的简单逻辑。</p> <h4 id="示例-使用星号-打印正方形">示例 7:使用星号 (*) 打印正方形。</h4> <p><strong>代码:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500627317712.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>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.</p> <h4 id="Example-Printing-rectangle-using-stars">Example 8: Printing rectangle using stars (*).</h4> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500627438442.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>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.</p> <h4 id="Example-Printing-a-Diamond-using-stars">Example 9: Printing a Diamond using stars.</h4> <p>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.</p> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">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; i0; i--) { //innermost loop for the space present in the inverted pyramid for (j=1; j <p>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.</p> <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500627714497.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <h4 id="Example-Printing-binary-numbers-in-a-stair-format">Example 10: Printing binary numbers in a stair format.</h4> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">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 <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500627989535.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的模式" ></p> <p>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.</p> <h4 id="Example-Program-to-print-repeating-alphabet-patterns">Example 11: Program to print repeating alphabet patterns.</h4> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">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=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中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

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

Atom编辑器mac版下载
最流行的的开源编辑器

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