0

I want to make a program to accept strings and make a pyramid of strings, but I cant make the right loop, I have the logic:
But this seems to make only half- pyramid, can somebody help me with the right loop

import java.util.Scanner;
public class Stairs
{
    public void main()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string");
        String a = sc.nextLine();
        int x, y;
       for (x = 0; x < a.length(); x++)
       {
            for (y = 0; y <= x; y++)
            {
                char c = a.charAt(y);
                System.out.print(c);
            }
            System.out.println();
       }
   }
}


        for (y = 0; y <= x; y++)
        {
            char c = a.charAt(y);
            System.out.print(c);
        }
        System.out.println();
   }

Edited by preyas.hanche

2
Contributors
3
Replies
21
Views
3 Years
Discussion Span
Last Post by stultuske
0

can you be a bit more clear on what it is you try to do, give for instance an example of 'input= ... , expected output = ... '

0

eg: input = java, then j on first level, aaa on second level, vvvvv on third level, aaaaaaa on fourth level (as you can see how i did it in the notepad0

Attachments
j
    aaa
   vvvvv
  aaaaaaa
0

before starting to print, calculate the length of the string you entered, then you know the width the 'biggest' String. make sure your first has half of that length in spaces before you print the 'j'
calculate about the same way for the other rows

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.