简体   繁体   English

在 Java 中打印简单模式

[英]Printing Simple Patterns in Java

Could someone explain the basics behind printing simple patterns in Java?有人能解释一下用 Java 打印简单模式背后的基础知识吗?

I'll give one specific example.我举一个具体的例子。

I'd just like for someone to clarify what each line is doing so I get a better understanding of how this works.我只是希望有人澄清每条线路在做什么,以便我更好地了解这是如何工作的。 Any other explained examples (line by line) would also be appreciated!任何其他解释的示例(逐行)也将不胜感激!

public static void drawPyramidPattern() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5 - i; j++) {
            System.out.print(" ");
        }
        for (int k = 0; k <= i; k++) {
            System.out.print("* ");
        }
        System.out.println();
    }
}

Printing anything or everything via a loop is just about understanding the flow of execution.通过循环打印任何或所有内容只是为了理解执行流程。 In your code also, if you'll start watching the flow line by line you'll come to know that how it is working exactly.在你的代码中,如果你开始一行一行地观察流程,你就会知道它是如何工作的。

If you understand how it works, you would be able to print any pattern, but basics should be clear.如果您了解它的工作原理,您将能够打印任何图案,但基础知识应该很清楚。 Try printing variable i, j and k values after each iteration.尝试在每次迭代后打印变量 i、j 和 k 值。 See the values that how that gets changed after each cycle of execution and then see the logic you've applied.查看在每个执行周期后如何更改的值,然后查看您应用的逻辑。

Your question is somewhat very broad in scope and can not be answered exactly unless narrowed it down.您的问题范围有点广泛,除非缩小范围,否则无法准确回答。 I would suggest to run this line by line and watch the output, try more changes even if it doesn't make any sense, you'll be having a good understanding over looping even for all of your future tasks.我建议一行一行地运行并观察输出,即使没有任何意义也尝试更多更改,即使对于未来的所有任务,您也会对循环有很好的理解。 And if after trying yourself, you come to any problem, share here, people are ready to solve them.如果在尝试自己之后遇到任何问题,请在此处分享,人们已准备好解决它们。 :) :)

Hope this helps.希望这可以帮助。

First you must a have complete understanding of loops, nested loops then you come up to patterns designing.首先,您必须完全了解循环、嵌套循环,然后才能进行模式设计。

1) First run the loops in hard form like on Register/on Page for understanding the loops. 1)首先以硬形式运行循环,如在注册/页面上理解循环。
2) Use debugger to identify the loop progress. 2) 使用调试器识别循环进度。

If you think about it in terms of mathematics, loops are just functions.如果从数学的角度考虑,循环只是函数。

A single for loop would just be x.单个 for 循环就是 x。

Example例子

for (int i = 0; i < 5; i++) {
System.out.println("This is function x.");
}

However when you start nesting loops it because a greater function.但是,当您开始嵌套循环时,因为它具有更大的功能。 A for loop inside another for loop would be a function x^2另一个 for 循环内的 for 循环将是一个函数 x^2

For example:例如:

for (int i = 0; i < 5; i++) {
     for (int j = 0; J < 5; j++){
     System.out.println("This is the j loop");
     }
     System.out.println("This is the i loop");
}

The reason behind this is because in order to finish the first iteration of i, everything inside the loop must be completed.这背后的原因是,为了完成 i 的第一次迭代,必须完成循环内的所有内容。 But, the i loop has another loop inside of it, so that must be finished first.但是, i 循环内部有另一个循环,因此必须先完成它。 So the loop with j must execute until it is finished.所以带有 j 的循环必须执行直到它完成。 (In this case 5 times), Great, now we can increment i. (在这种情况下是 5 次),太好了,现在我们可以增加 i。 But now we have to step through j again!但是现在我们必须再次通过 j! This process continues until i reaches its threshold of being < 5. So the output would look something like this这个过程一直持续到 i 达到它的阈值 < 5。所以输出看起来像这样

Output:
This is the j loop
This is the j loop
This is the j loop
This is the j loop
This is the j loop
This is the i loop
This is the j loop
This is the j loop
....

This would continue until the i has reached 5, in which case it no longer satisfies the necessary i < 5, and the loop would end.这将一直持续到 i 达到 5,在这种情况下,它不再满足必要的 i < 5,并且循环将结束。 Hopefully this helps希望这会有所帮助

First, since i = 0 & 0<5 is true you enter the first(outer) for-loop.首先,由于 i = 0 & 0<5 为真,因此您进入第一个(外部)for 循环。

Remember i = 0.记住 i = 0。

Then j = 0;则 j = 0; but 0 < i = 0 is false so you don't enter the second loop.但是 0 < i = 0 是假的,所以你不要进入第二个循环。

For the third loop, k = 0 & 0<=0 is true.对于第三个循环,k = 0 & 0<=0 为真。 So you enter the loop and execute the print statement, ie print a star.所以你进入循环并执行打印语句,即打印一个星号。

k++, this will increment k by 1 and check the boolean; k++,这会将 k 增加 1 并检查布尔值; You ask yourself is 1 <= 0;你问自己是 1 <= 0; clearly no ;显然没有; so you exit the for-loop and then reach the println statement which will take you to the next line.所以你退出 for 循环,然后到达 println 语句,它将带你到下一行。

And then you go back to the outer loop.然后你回到外循环。

//this code print Diagonal Pattern if matrix is 1 2 3 4 5 6 7 8 9 output is : 1 4 2 7 5 3 8 6 9 //如果矩阵为 1 2 3 4 5 6 7 8 9 则此代码打印对角线模式输出为:1 4 2 7 5 3 8 6 9

import java.util.*;
class DiagonalPattern
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int x[][];
        int i,j,row,col,p,temp=1,last=0;
        System.out.println("how many array wants to create and size of array");
        row=sc.nextInt();
        col=sc.nextInt();
        x=new int[row][col];
        System.out.println("Enter  " +row*col+ "  elements of array of array");
            for(i=0;i<row;i++)
            {
            for(j=0;j<col;j++)
            {
                x[i][j]=sc.nextInt();
                last=j;
            }
            }
        for(i=0;i<row;i++)
            {
            System.out.println("");
            int k=i;
            for(j=0;j<=i;j++,k--)
            {
                if(j==col)
                {
                    break;
                }
                else
                {
                    System.out.print(x[k][j]);  
                System.out.print(" ");
                }
            }
            }
        for(p=x.length;p>0;p--,temp++)
            {
            System.out.println("");
            i=x.length-1;
            int k=i;
            for(j=temp;j<=last;j++,k--)
            {
                System.out.print(x[k][j]);  
                System.out.print(" ");
            }
            }   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM