简体   繁体   English

Java使用for循环打印某些模式

[英]Java Print certain pattern using for loop

below are my code can anyone help me i stuck at the * part 以下是我的代码,有人可以帮我吗?

i want the output like this 我想要这样的输出

     9
    989
   98789
  9876789
 987656789

... and so on ... 等等

    for (int i = 1; i <= l; i++) {
        for (int j = 1; j <= l - i; j++)
            System.out.print(" ");

        for (int j = 1; j <= (2 * i - 1); j++) {
            if((l-j) < l-i){
            // here System.out.print("*");
            }
            else
                System.out.print(l - j);

        }
        System.out.print("\n");
    }

I ended up rewriting it; 我最终重写了它; I couldn't figure out a concise constant for the print you were missing. 我无法弄清楚您所缺少的print的简洁常数。 If you loop i from 0 to l (inclusive), you can do: 如果将i0循环到l (含),则可以执行以下操作:

Spaces 空间

The last line ( i = l ) has 0 spaces, so each line has li spaces: 最后一行( i = l )有0个空格,所以每行有li空格:

for(int j = 0; j < l-i; j++)
    System.out.print(" ");

Left/Center 左/中

Every line starts at l , and you want to count down till you reach the center; 每行从l开始,并且您想倒数直到到达中心; since the current line has li spaces and the center is at l+1 (since I included the line with a 0 center), which is i+1 away, you need to output through li inclusive 由于当前行具有li空格,并且中心位于l+1 (因为我包含的中心为0的行)(即i+1处),因此您需要通过li inclusive输出

for(int j = l; j >= l-i; j--)
    System.out.print(j);

Right

The same loop as above, but reversed. 与上述相同的循环,但是相反。 Starts 1 higher because the center is already handled above and you don't want to output it again 因为中心已经在上面处理了,所以您从更高的位置开始1,并且您不想再次输出它

for(int j = l-i+1; j <= l; j++)
    System.out.print(j);

I'd rework a little more than just that one line 我要做的工作不只是那一行

    for (int j = 1; j <= (2 * i - 1); j++) {
        if((l-j) < l-i){
        // here System.out.print("*");
        }
        else
            System.out.print(l - j);

To... 至...

for(int j = 1; j < i; j++)
     System.out.print(10-j);

System.out.println(10-i);

for(int j = 1; j < i; j++)
     System.out.print(10-i+j);    

This is completed program. 这是完成的程序。 I used "divide and conquer" method (basically the same idea as of Michael Mrozek), and use method to make it extreme-easy-to-understand program . 我使用了“分而治之”的方法(基本上与Michael Mrozek的想法相同),并使用该方法使其变得非常易于理解。 Hope this will help. 希望这会有所帮助。

public class PatternPrinter {
public static void main(String[] args) {
    printAllRows(9, 5);
}

public static void printAllRows(int max, int numOfRows) {
    for (int i = 1; i <= numOfRows; i++) {
        printRow(numOfRows - i, max, i);
        System.out.print("\n");
    }
}

public static void printRow(int numOfSpaces, int max, int num) {
    printSpaces(numOfSpaces);
    printLeftAndCenterPart(max, num);
    printRightPart(max, num - 1);
}

public static void printRightPart(int max, int num) {
    while(num > 0) {
        System.out.print(max - num + 1);
        num--;
    }
}

public static void printLeftAndCenterPart(int max, int num) {
    while(num > 0) {
        System.out.print(max--);
        num--;
    }
}

public static void printSpaces(int numOfSpaces) {
    while(numOfSpaces > 0) {
        System.out.print(" ");
        numOfSpaces--;
    }
}

} }

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

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