首页  >  文章  >  Java  >  Java 中的模式

Java 中的模式

PHPz
PHPz原创
2024-08-30 16:24:151096浏览

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

输出:

Java 中的模式

在上面的示例中,只需要 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();
}
}
}

输出:

Java 中的模式

 

在上面的示例中,我们需要将箭头分成两半,并为每一半使用 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();
}
}
}

输出:

Java 中的模式

在上面的示例中,我们需要做 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();
}
}
}

输出:

Java 中的模式

简单的半金字塔很容易,因为我们需要处理数字、*或我们正在打印的字符,但对于反向金字塔,我们需要首先打印空格,然后打印模式,在我们的例子中是 (*) 。因此使用了 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();
}
}
}

输出:

Java 中的模式

金字塔的打印逻辑与上面示例中使用的逻辑相同,使用 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();
}
}
}

输出:

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 中的模式的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn