0

I'm going through some introductory exercises and I can not understand how to get java to output a string of five letters in the particular pattern shown below. said pattern

Initially I thought it followed the tribonacci sequence for number of characters per line. Without just printing the line, I can not figure out how to have java logically replicate the pattern. They seem to copy each other, but don't really follow a pattern.

3
  • 1
    what is your input? What have you tried so far? Commented Sep 14, 2016 at 0:19
  • So I have worked with the classic pyramid builds but they go in a linear fashion, while this doesn't. Some initial attempts have been with stating each letter as a string, than adding each one to the previous, but that only increases by one as well. Commented Sep 14, 2016 at 0:25
  • This would be an ideal problem to solve using recursion (have a look at "recursive functions") Commented Sep 14, 2016 at 1:04

2 Answers 2

3

The strings are palindrome and getting its end from the last string, for example; line 2 has "ABA" string, so line 3 will copy "ABA" at its end and will insert character C in the middle, so the final string will be "ABACABA"

String LastPattern="";
        for(int i=0;i<5;i++){
            System.out.println( LastPattern + (char)(65+i) +LastPattern);
            LastPattern=LastPattern + (char)(65+i) +LastPattern;
        }
Sign up to request clarification or add additional context in comments.

8 Comments

Hint. just dropping code without any explanations: doesnt make a good answer!
That's fantastic, but yeah if you could have some explanation of the logic behind why/how it's done that way that would be fantastic. I'd be happy to mark it as an answer then.
The strings are palindrome and copying getting its end from the last string, for example; line 2 has "ABA" string, so line 3 will copy "ABA" at its end and will insert character C in the middle, so the final string will be "ABACABA".
@fatmi Update your answer with this comment
does this explanation help now?
|
1

Maybe this gets you going:

Something

Something New Something

Something New Something Newer Something New Something

The pattern is there, right in front of you.

3 Comments

Yeah you're right. I'm just legitimately too new at this to be able to understand it. If this is a wasted post I'll delete it.
I suppose. I just wish I could better articulate my question so that it's not a waste of space on here but I don't know enough to properly ask in this language.
You're creating palindromes around an increasing character if you wanted to be more clear on what you're doing @bonzo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.