使用 for 循环在 Java 中打印模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2326680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:37:36  来源:igfitidea点击:

Using a for loop to print a pattern in Java

javafor-loop

提问by Dan

I am trying to print out this pattern using a for loop in Java but I am kind of stuck.

我正在尝试使用 Java 中的 for 循环打印出这个模式,但我有点卡住了。

zzzzz   
azzzz  
aazzz  
aaazz  
aaaaz  
aaaaa  

I can print:

我可以打印:

 a  
 aa  
 aaa  
 aaaa  
 aaaaa  

using:

使用:

String i = " ";   
int a = 0;  
for (i="a";i.length()<=5;i=i+"a")  
    System.out.println(i);

and

 zzzzz  
 zzzz  
 zzz  
 zz  
 z  

using:

使用:

String i = " ";   
for (i="zzzzz";i.length()>0;i=i.substring(0,i.length()-1))  
    System.out.println(i); 

But I can't figure out how to combine them. I was thinking about replacing the substring of iand increasing the value of the end index by one everytime but not sure of to code it. I started with something like this:

但我无法弄清楚如何将它们结合起来。我正在考虑i每次替换子字符串并将结束索引的值增加一但不确定对其进行编码。我从这样的事情开始:

String i = " ";  
String b = " ";  
for (i="zzzzz";i="aaaaa";i=i.replace(i.substring(0,))  
    System.out.println(i);  

Any ideas?

有任何想法吗?

回答by missingfaktor

Pseudocode :

伪代码:

for(i <- 0 to 5) {
  print( i times "a" followed by (5 - i) times "z")
  print a new line
}

Now implement this in Java.

现在在 Java 中实现它。

回答by Ledhund

You can increment or decrement more than one variable with the loop

您可以使用循环增加或减少多个变量

for (int a = 0, z = 5; a <= 5 ; a++, z-- )
{
  System.out.println(a+" "+z);
}

would output

会输出

0 5
1 4
2 3
3 2
4 1
5 0

回答by apchester

In java:

在Java中:

public class Pattern {

    public static void main(String [] args) {

        for(int i=0;i<6;i++) { //This works out the number of lines
            String line = "";
                for(int a=0;a<i;a++) {
                    line+="a";
                }

                for(int z=0;z<(5-i);z++) {
                    line+="z";
                }

                System.out.println(line);       
        }

    }

}

回答by user280592

String AA = "aaaaa";
String ZZ = "zzzzz";

for (int i = 0; i <= 5; i++) {
    System.out.println(AA.substring(i) + ZZ.substring(5 - i));
}

回答by IVlad

Z = 5
A = 0

while ( Z >= 0 )
{
  for ( i = 0; i < A; i++ ) print 'A';
  for ( i = 0; i < Z; i++ ) print 'Z';
  print newline;
  ++A;
  --Z;
}

is one way.

是一种方式。

回答by Roman

Use one additional variable to keep position of a/z border. Increase value of that variable in each iteration.

使用一个额外的变量来保持 a/z 边界的位置。在每次迭代中增加该变量的值。

回答by ADith

You might try the following:

您可以尝试以下操作:

public class pattern2
{
    public static void main()
    {
        int i,j,k,num=0;
        for(i=1;i<=6;i++)
        {
            for(j=1;j<=num;j++)
            System.out.print("a");
            for(k=6;k>i;k--)
                System.out.print("z");
            System.out.println();
            num++;
        }
    }
}