Dave Tolls wrote:That is caused by what Carey said in the last line of his post.
You are looping over the Entries in the Map and returning the first one only.
You need to decide whether getAnswers returns a pre-formatted String you can simply display to the user, or whether it returns the keySet (you can get the keys by themselves from a map without the values) and leaves it up to the calling code to do the layout. I would prefer the latter.
Liutauras Vilda wrote:1. Why do you have all your fields as public? You shouldn't be doing that, make them private.
2. Variables a1, a2, a3, what these are? Answers.. You know, but someone else will need to read all your code to find out that.
Don't try to save space in the price of readability. Every variable has to tell you right away, what he is meant to store. We know about i and j because these are conventional for the loop iterations, but a1, a2 are not.
3. Code below. Apart from the missing curly braces around if statement block, formatting is also incorrect. for loop suppose to be shifted by 4 spaces to the right side. Again, everything goes down to the code readability, omitting the fact, that you could miss something important and that code could behave wrongly because of simply missing curly braces.
Actually there is something wrong with this code. Two mistakes I see. Method name suggests that it should return the list, but it appears it is returning not the list, but array element (single), not whole array. Also similar formatting problems in other method too.
That is it at the moment from me.
Carey Brown wrote:When your problem manifests itself as a thrown exception, please make sure to cut and paste the stack trace in your post. And also make sure you have provided a listing of code that includes the line indicated by the stack trace.
Nit: You have a variable called 'list' which is not a list but an array. Better yet, name it for what the variable contains, e.g. 'answers', or something like that.
You have several loops with a return that is not enclosed in an if(), therefore, only the first element will ever be returned.
Campbell Ritchie wrote:It is not a good idea to decide to use a database before you decide what you want to store. Have you considered a question class with question and answers associated encapsulated? You can include wrong answers and randomise the order the answers appear in to make it less predictable.
Henry Wong wrote:Joe, Nice diagram. You earned a cow.
BTW, in reading this thread again, I think the OP is thinking that the results should be the sum of 8, 4, 2, 1; ie. the sum of the parameters, and not what was returned from the method call. Or in other words, the OP was thinking about the value of "n/2" and not the value returned from the inverse2power(n/2) method call?