This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am trying to build my first program, and had an idea for a sort of "Magic 8 Ball" thing, where you enter a question and the program outputs a random answer. However, I'm having a great deal of trouble figuring it out. I know I could do it using an array to store the answers and the Math.rand operator to output a random answer, but I can't figure out how to work the Scanner in, or how to make it all work together, and I'm not sure if I can get it to work with just one Class. Please help.
Below is an example of the code I intend to use to generate the answers.
Andrew McCarthy wrote:I can't figure out how to work the Scanner in, or how to make it all work together,
What specific problems are you having with it?
and I'm not sure if I can get it to work with just one method.
Why would you want to? One method should do one thing. For a very rough example here, one method to take user input, one method to get the random answer, one method to output the answer. You might end up breaking those methods down further, or possibly combining a couple of them, but "trying to do it in one method" is not a worthwhile programming goal.
Okay, well, again, we don't generally try to force something into a single class, although in your case you shouldn't need more than one.
So, if you can show what you've got that works, and then what you tried to do next, and explain exactly what problems you had with that, people here will be happy to help. You need to TellTheDetails(←click) so that we can understand your problem.
Joined: Jun 20, 2012
Well, I was able to get it to work. Below is the finished code. I would welcome some comments.