nested for loop for alphabet increment

2019-09-19 12:02发布

I have searched everywhere and I cannot figure out how to create this output using a nested for loop in java:

"a

ab

abc

abcd"

continued until z

this is what I have tried

String alphabet = "abcdefghijklmnopqrstuvwxyz";

    for(int i = 0; i <= 25; i++)
    {
        for(char j = (char)(alphabet.charAt(i)); j<=i; j++)
        {

        System.out.print(j);
        }
        System.out.println();
    }

Please help me!

2条回答
混吃等死
2楼-- · 2019-09-19 12:25

Here you go !

print capital Alphabet

  public class Main
    {
  public static void main(String[] args)
    {
        int i, j;
        for(i = 1; i<=26; i++)  {
        for(j = 1; j <= 1-i; j++) {
        System.out.print(" ");
        for(j = 1; j <= i; j++) 
        System.out.print((char)(char)(i+64)+" ");
        System.out.println(); //for line break
          }
      }
  }
查看更多
干净又极端
3楼-- · 2019-09-19 12:33

Here is the answer:

    for (int x = 'a'; x<='z'; x++)
   {
        for (int i = 'a'; i<=x; i++)
        {
          System.out.print((char)i);
        }
        System.out.println();

    }

The outer loop switches from one row to another whilst the inner loop prints the characters for that particular row.

查看更多
登录 后发表回答