Campbell Ritchie wrote:Welcome to the Ranch
You are asking two questions in the thread title, which are completely different. Only worry about “efficiency” if you have some sort of evidence that the program is running inefficiently. There are various simple thing syou can do, however. I think you should use few print calls and make them larger. You can probably run a 200‑letter String together in 0.2μs and then take a few milliseconds to print it on screen. You can change lines 31‑35 to one line; familiarise yourself with the printf method (there are several other methods scattered around which behave very similarly) and you will find it very useful:-You have four %ns for new line and two %ss for String representation, so I think I have compressed those 5 lines correctly. Note how I broke the line into shorter lines. You will also note I removed the// print key comment, which adds nothing.
As for speeding up the code, I would suggest what else you should do about performance just at the present: nothing. Much more important to make the code correct first. So I shall say little about formatting, though you should make lots of formatting changes.
Campbell Ritchie wrote:I am afraid the structure of the program is confused, . . .