Okay, so we were told to make a program that takes user inputted word and then formats it so it repeats vertically and horizantally but also adjusts one character each time.
Kind of like this.
Word: MARY
MARY
ARYM
RYMA
YMAR
The word can be as long as the user desires.
The instructions don't say to use a specific type of loop, just as long as nested loops are used.
I did a for loop but my professor said that he believed that a charAt method would be easier where each letter was assigned a number and then the number was adjusted each time. I didn't really understand his approach so it's hard for me to repeat it. My professor said that the problem was simple and I was over-thinking it. I was leaning towards using two for loops, one for the inside and one for the outside. I had a for loop that will do the following output:
MARY
ARY
RY
R
So i'm trying to figure out how to add the remaining letters.
I've tried a couple of different things so i've commented out what hasn't worked.
//Assign Variables and Accept input from the keyboard
String sentence;
int min=0; int max;int k = 0; int differenceinlength;
Scanner keyboard= new Scanner (System.in);
System.out.println("Enter a string: ");
sentence = keyboard.next();
max= sentence.length();
int i=sentence.charAt(0);
for (i=0; i<=max; i++){
System.out.println(sentence.substring(i));
//if (sentence2.length()<sentence.length())
//k=sentence2.length();
//differenceinlength=i-k;
//int characteratdiff=sentence2.charAt(differenceinlength);
//int j=sentence2.charAt(0);
//for (j=0; j<=max; j++)
//{ System.out.println(sentence.substring(i)+sentence2.substring(characteratdiff));}
}}}
This gives me
MARY
ARY
RY
R
as a output.
If you could please tell me what direction to go in I would really appreciate it!
Sincerely,
Overwhelmed

New Topic/Question
Reply



MultiQuote







|